remove expr ambiguity

This commit is contained in:
User 2017-05-12 18:13:12 +02:00
parent 1f8008717b
commit e5ab8b2db7
4 changed files with 340 additions and 324 deletions

View File

@ -1,23 +1,26 @@
grammar Basic;
program: expr;
program: expr EOF;
expr
: singleExpr (COMPOUND singleExpr?)*
;
singleExpr
: PAROPEN expr PARCLOSE #parens
| BRAOPEN expr BRACLOSE #block
| IN PAROPEN variable (LISTDELIM variable)* PARCLOSE #read
| OUT PAROPEN expr (LISTDELIM expr)* PARCLOSE #write
| IFOPEN cond=expr IFTRUE onTrue=expr (IFFALSE onFalse=expr)? IFCLOSE #if
| WHILEOPEN cond=expr WHILETRUE onTrue=expr WHILECLOSE #while
| op=(PLUS|MINUS|NOT) expr #prefix1
| lhs=expr op=(MULTIPLY|DIVIDE|MODULO) rhs=expr #infix1
| lhs=expr op=(PLUS|MINUS) rhs=expr #infix2
| lhs=expr op=(LT|LEQ|GTE|GT|EQ|NEQ) rhs=expr #infix3
| lhs=expr AND rhs=expr #infix4
| lhs=expr OR rhs=expr #infix5
| op=(PLUS|MINUS|NOT) singleExpr #prefix1
| lhs=singleExpr op=(MULTIPLY|DIVIDE|MODULO) rhs=singleExpr #infix1
| lhs=singleExpr op=(PLUS|MINUS) rhs=singleExpr #infix2
| lhs=singleExpr op=(LT|LEQ|GTE|GT|EQ|NEQ) rhs=singleExpr #infix3
| lhs=singleExpr AND rhs=singleExpr #infix4
| lhs=singleExpr OR rhs=singleExpr #infix5
| DECLARE type IDENTIFIER #declare
| <assoc=right> variable ASSIGN expr #assign
| expr (COMPOUND expr?)+ #compound
| <assoc=right> variable ASSIGN singleExpr #assign
| variable #var
| LITERAL10 #number
| CHAR #char

View File

@ -18,6 +18,13 @@ public class BasicBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitProgram(BasicParser.ProgramContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitExpr(BasicParser.ExprContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
@ -81,13 +88,6 @@ public class BasicBaseVisitor<T> extends AbstractParseTreeVisitor<T> implements
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitWhile(BasicParser.WhileContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*
* <p>The default implementation returns the result of calling
* {@link #visitChildren} on {@code ctx}.</p>
*/
@Override public T visitCompound(BasicParser.CompoundContext ctx) { return visitChildren(ctx); }
/**
* {@inheritDoc}
*

File diff suppressed because it is too large Load Diff

View File

@ -16,135 +16,134 @@ public interface BasicVisitor<T> extends ParseTreeVisitor<T> {
* @return the visitor result
*/
T visitProgram(BasicParser.ProgramContext ctx);
/**
* Visit a parse tree produced by {@link BasicParser#expr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitExpr(BasicParser.ExprContext ctx);
/**
* Visit a parse tree produced by the {@code parens}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitParens(BasicParser.ParensContext ctx);
/**
* Visit a parse tree produced by the {@code read}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitRead(BasicParser.ReadContext ctx);
/**
* Visit a parse tree produced by the {@code declare}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitDeclare(BasicParser.DeclareContext ctx);
/**
* Visit a parse tree produced by the {@code bool}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitBool(BasicParser.BoolContext ctx);
/**
* Visit a parse tree produced by the {@code var}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitVar(BasicParser.VarContext ctx);
/**
* Visit a parse tree produced by the {@code infix2}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitInfix2(BasicParser.Infix2Context ctx);
/**
* Visit a parse tree produced by the {@code infix3}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitInfix3(BasicParser.Infix3Context ctx);
/**
* Visit a parse tree produced by the {@code infix1}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitInfix1(BasicParser.Infix1Context ctx);
/**
* Visit a parse tree produced by the {@code while}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitWhile(BasicParser.WhileContext ctx);
/**
* Visit a parse tree produced by the {@code compound}
* labeled alternative in {@link BasicParser#expr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitCompound(BasicParser.CompoundContext ctx);
/**
* Visit a parse tree produced by the {@code infix4}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitInfix4(BasicParser.Infix4Context ctx);
/**
* Visit a parse tree produced by the {@code infix5}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitInfix5(BasicParser.Infix5Context ctx);
/**
* Visit a parse tree produced by the {@code number}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitNumber(BasicParser.NumberContext ctx);
/**
* Visit a parse tree produced by the {@code char}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitChar(BasicParser.CharContext ctx);
/**
* Visit a parse tree produced by the {@code block}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitBlock(BasicParser.BlockContext ctx);
/**
* Visit a parse tree produced by the {@code write}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitWrite(BasicParser.WriteContext ctx);
/**
* Visit a parse tree produced by the {@code if}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitIf(BasicParser.IfContext ctx);
/**
* Visit a parse tree produced by the {@code prefix1}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/
T visitPrefix1(BasicParser.Prefix1Context ctx);
/**
* Visit a parse tree produced by the {@code assign}
* labeled alternative in {@link BasicParser#expr}.
* labeled alternative in {@link BasicParser#singleExpr}.
* @param ctx the parse tree
* @return the visitor result
*/