谭子悦 201250093

Basic Idea

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:

  1. Write the correspoding parser rules in SysYParser.g4
  2. Generate SysYParser.g4.java with ANTLR
  3. Write the application code with Parser API and ANTLR Runtime API

Details

Iterating through non-terminals and terminals

The 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:

  1. Implement accept(Visitor) in superclass, which also provides the default operations to do for non-specific types.
  2. Override accept(Visitor) in target subclasses, in which the corresponding handler in Visitor will be called. For example, Foo.accept(visitor) will call visitor.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.

Left-recursion

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.