I'm trying to create a grammar which allows certain expressions only as part of an expression and not at the very beginning of an expression statement. In the grammar below those certain expressions are arrow function expression and dot expression.
For example these expressions are valid:
a = b => c; a and b => c; x + y and z; x + y . z; while these are invalid (the first starts with a dot-expression, and the second is not part of expr nor assignment):
x . y + z; b => c; I tried a grammar like this:
parser grammar ExprParser; options { tokenVocab=ExprLexer; } program : (stat | def)+ EOF ; stat: ID '=' expr ';' | startExpr ';' | func ';' ; def : ID '(' ID? (',' ID)* ')' '{' stat* '}' ; arrowExpr : ID '=>' expr ; startExpr : atom | 'not' expr | startExpr '+' expr | startExpr 'and' expr | startExpr 'or' expr ; expr: atom | arrowExpr | 'not' expr | expr '+' expr | expr 'and' expr | expr 'or' expr | expr '.' expr ; atom: ID | INT | func ; func: ID '(' ID? (',' ID?)* ')'; lexer grammar ExprLexer; AND : 'and' ; OR : 'or' ; NOT : 'not' ; EQ : '=' ; COMMA : ',' ; SEMI : ';' ; LPAREN : '(' ; RPAREN : ')' ; LCURLY : '{' ; RCURLY : '}' ; PLUS : '+' ; MINUS : '-'; ARROW : '=>'; DOT : '.'; INT : [0-9]+ ; ID: [a-zA-Z_][a-zA-Z_0-9]* ; WS: [ \t\n\r\f]+ -> skip ; but this approach messes up operator precedence. x + y and z; should produce
but instead produces
Any ideas on how to solve this?


