Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

HI I need help with Antlr4 and Intelliji essentially I need an explanations on t

ID: 3869186 • Letter: H

Question

HI I need help with Antlr4 and Intelliji essentially I need an explanations on this.

I have a yacc(grammar) file that I have to make it okay to be fed into antlr. After getting there, I parse a program preferrably (Fibonacci) using what antlr gives us. Essentially Yacc and antlr grammars are almost the same, so we actually don't need to do much to the parser rules. We need to write the lexer rules, which are mostly just copying the definitions of Icon tokens from references but I don't really understand what this means.

I am really confused by this what are lexer rules and tokens in regards to creating a grammer that can parse lets say an arithmetic expression.

Below is a problem(the attached picture) used as an example but I am still super confused

"Parse arithmetic expressions into a tree using Antlr and using a grammar along the lines of the one I showed you on the whiteboard(The grammar is top right)"

The solution or grammer derived from this problem is this how do I get to here from antlr I am so lost.

grammar Arithmetic;

expr: term | term '+' expr ;
term: factor | factor '*' term ;
factor: NUMBER | '(' expr ')' ;

WS : [ ]+ -> skip ;
NUMBER: [0-9]+ ;

e yopr @sr) (tera.) t. ( epr lexical Se char ant ir terw

Explanation / Answer

CharStream input = CharStreams.fromPath(Paths.get(args[0])); ExprLexer lexer = new ExprLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); ExprParser parser = new ExprParser(tokens); parser.getInterpreter().setPredictionMode(PredictionMode.SLL); try { parser.stat(); // STAGE 1 } catch (Exception ex) { tokens.reset(); // rewind input stream parser.reset(); parser.getInterpreter().setPredictionMode(PredictionMode.LL); parser.stat(); // STAGE 2 // if we parse ok, it's LL not SLL }