CAP/TP02/demo_files/ex3/Example3.g4

19 lines
557 B
Plaintext
Raw Normal View History

2024-09-23 11:04:28 +02:00
//define a tiny grammar with attributes for arith expressions with identifiers
grammar Example3;
full_expr: e0=expr ';' EOF {print($e0.text + " has " + str($e0.count) + " operators!")} ;
expr returns [int count]: // expr has an integer attribute called count
| e0=expr OP e1=expr {$count = $e0.count + $e1.count + 1} // name sub-parts and access their attributes
| ID {$count = 0}
| INT {$count = 0}
;
OP : '+'| '*' | '-' | '/' ;
INT : '0'..'9'+ ;
ID : ('a'..'z'|'A'..'Z')+ ;
WS : [ \t\r\n]+ -> skip ; // skip spaces, tabs, newlines