I am trying to write a basic grammar that starts with '{' and ends with '}' . It has 'IF' and 'Else' only. And some basic expressions like a = (arithmetic operation). There is no need for variable initialisation and declaration check. Can someone guide me, I am writing grammar, but it is not working properly. I am writing code below of my grammar.
prog: stat_block+ EOF;
stat_block
: OBRACE block CBRACE
;
block
: stat*
;
stat: expr
;
expr: expr ('*'|'/') expr
| expr ('+'|'-') expr
| expr ('<'|'<='|'>='|'>'|'=') expr
| expr ( '&&'|'||') expr
| expr '(' exprList? ')'
| IF condition_block (ELSE stat_block)?
;
exprList : expr (',' expr)* ;
condition_block
: OPAR expr CPAR stat_block
;
IF : 'IF';
ELSE : 'ELSE';
OPAR : '(';
CPAR : ')';
OBRACE : '{';
CBRACE : '}';
ID : [a-zA-Z]+ ;
INT : [0-9]+ ;
NEWLINE:'\r'? '\n' ;
WS : [ \t]+ -> skip ;
Sample Code for what i am trying to write grammar for
{
IF ( a > 10 && funcName(param) = Found ) {
b = 10;
}
ELSE {
b=20;
}
}
If statement can be nested. IF some one can provide me with grammar or tell me, what i am doing wrong. that will be huge help. thanks.
With the following changes, I get an error free parse and a parse tree:
1.
NEWLINE:'\r'? '\n' ;
You're not using the NEWLINE
token, so, presumably, you want to hide that token from the parser rules. -> skip
is one simple way to accomplish that.
NEWLINE:'\r'? '\n' -> skip;
expr
: expr ('*' | '/') expr
| expr ('+' | '-') expr
| expr ('<' | '<=' | '>=' | '>' | '=') expr
| expr ( '&&' | '||') expr
| expr '(' exprList? ')'
| IF condition_block (ELSE stat_block)?
;
ID
s and NUMBER
s should also be considered expressions
expr
: expr ('*' | '/') expr
| expr ('+' | '-') expr
| expr ('<' | '<=' | '>=' | '>' | '=') expr
| expr ( '&&' | '||') expr
| expr '(' exprList? ')'
| IF condition_block (ELSE stat_block)?
| ID
| INT
;
block
: stat*
;
stat: expr
;
You haven't accounted for the possibility/requirement of the ;
after an expression
It's not clear you intention. so
To simply say that each expression may be followed by a ;
stat: expr ';'?;
This would allow for some possible confusion when expressions appear adjacent to one another without a ;
, so consider making it non-optional.
stat: expr ';';
Many language treat ;
as a expression/statement separator.
block: (stat (stat ';')*)?;
The sum of the changes:
grammar Basic
;
prog: stat_block+ EOF;
stat_block: OBRACE block CBRACE;
block: (stat (stat ';')*)?;
stat: expr ';'?;
expr
: expr ('*' | '/') expr
| expr ('+' | '-') expr
| expr ('<' | '<=' | '>=' | '>' | '=') expr
| expr ( '&&' | '||') expr
| expr '(' exprList? ')'
| IF condition_block (ELSE stat_block)?
| ID
| INT
;
exprList: expr (',' expr)*;
condition_block: OPAR expr CPAR stat_block;
IF: 'IF';
ELSE: 'ELSE';
OPAR: '(';
CPAR: ')';
OBRACE: '{';
CBRACE: '}';
ID: [a-zA-Z]+;
INT: [0-9]+;
NEWLINE: '\r'? '\n' -> skip;
WS: [ \t]+ -> skip;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.