谭子悦 201250093
The basic goal of Lab2 is to write a parser for SysY with ANTLRv4, and implement a simple syntax highlight function.
The main procedure includes:
SysYParser.g4
SysYParser.g4.java
with ANTLRThe parse tree can be printed with use of Visitor Pattern.
The goal of using Visitor Pattern is to avoid using verbose and error-prone if-else statements to do type-specific operations.
The basic idea is:
- Implement
accept(Visitor)
in superclass, which also provides the default operations to do for non-specific types.- Override
accept(Visitor)
in target subclasses, in which the corresponding handler in Visitor will be called. For example,Foo.accept(visitor)
will callvisitor.handleFoo(Foo)
The visitor base class in ANTLR also provides more general handler for child nodes (non-terminals) and terminals. And hence we can override these methods to print information when traversing the tree.
The original specification of SysY parser rules is written in a manner without left-recursion. This is because left-recursion in Context-Free Grammar can leads to ambiguity. But ANTLR can deal with this ambiguity, by rewriting the rules with semantic predicates to introduce precedence. Therefore, to make the parse tree simpler and more human-friendly, we can rewrite the rules like cond
and exp
in left-recursive manners.