On Mon, 2 Apr 2001 22:30:56 +0900, Robert Feldt <feldt / ce.chalmers.se> wrote: >On Mon, 2 Apr 2001, hipster wrote: > >> ANTLR grammars are written in a kind of EBNF, which makes them very >> readable: >> >> rule >> : TOKEN >> | nonterminal >> | (optional)? >> | (zero_or_more)* >> | (one_or_more)? >> ; >> >Yeah, its great. Do you know if it is general (ie. you can apply operators >(?/+/*) to sequences of symbols and nest them or if they only applies to >individual symbols? (rockit has the latter but not the former) Generally speaking, the repetition modifiers ? + * should be able to apply to a group of symbols and rules in parentheses (ANTLR grammar and Perl's parse::recDescent both allow this). This is a very important feature to have to make grammars possible to write. Without it, you'll have to add *a lot* of intermediate rules to compensate for the lack of this feature. > >> semantic_predicates >> : { conditional_code; } => rule >> | { conditional_code; } => rule >> | foo >> ; >Ok, and you have access to parsing/lexing info when writing the >conditional_code? What kind of info? Can you access the AST being built? > >> automatic Abstract Syntax Tree generation with grammar annotations (^): >> >> mult_expr >> : add_expr ((STAR^ | SLASH^) add_expr) >> ; >> >> add_expr >> : atom ((PLUS^ | MINUS^) atom) >> ; >> >> so 1 + 2 * 3 gives (+ 1 (* 2 3)) (LISPy representations) >> >Interesting. In rockit I've used a different but similar thing: > >mult_expr: add_expr STAR add_expr [Mult: left, _, right] > | add_expr SLASH add_expr [Div: nom, _, denom] >add_expr: atom PLUS atom [Plus: left, _, right] > | atom MINUS atom [Minus: left, _, right] >atom: NUMBER [^] > >but as you see it's not as terse even though its more flexible. Maybe >should learn from ANTLR here... > >Thanks for the intro, > >Robert