www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - moving from classical lex/yacc to pegged parser

reply Dmitry Ponyatov <dponyatov gmail.com> writes:
Using lex/yacc I can do a more or less complex things in .yacc 
semantic actions, such complex as bytecode compilation or real 
CPU assembly.

Playing with `pegged`, I can't figure out how to move from 
`ParseTree` to such like semantic actions. I even can't parse 
numbers from strings in lexer-like rules because it looks like 
every rule runs on any token parse, or sumething like this.

Also, I use attribute object trees resemble attribute grammar 
both for parsing and internal code representation:

```C++
class Object {
   string               value; // or `int value` and `float value` 
for numbers
   map<string, Object*> attr;
   vector<Object*>      nested;
}
```

And I also can't figure out how to inherit `ParseTree` with all 
my script language objects to get AST right from pegged parser. 
Should I use some superloop with lot of matches to process parsed 
`pt` tree into something I need myself, to drop all unneeded 
parsing meta info and get clean semantic AST?
May 09
parent Dukc <ajieskola gmail.com> writes:
Dmitry Ponyatov kirjoitti 9.5.2024 klo 11.30:
 And I also can't figure out how to inherit `ParseTree` with all my 
script language objects to get AST right from pegged parser. Should I use some superloop with lot of matches to process parsed `pt` tree into something I need myself, to drop all unneeded parsing meta info and get clean semantic AST? Pegged can help you with that filtering part, at least to some extent. Remember you can use : and ; prefix operators in the grammar spec, and Pegged will drop the needless nodes for you. Or do the reverse, use ^ prefix operator (or write a new rule) to make a node out of Pegged builtins.
May 10