Homework 3
|
The text examines two programs that generate Java parsers: JavaCC and SableCC. Examples from the text for each generator will be given with instructions on their use. Chapter 3 notes provide greater details of using the two generators.
SableCC is the result of a master's degree project, the thesis forms the basic documentation. It can be obtained at: http://sablecc.org/downloads/thesis.pdf
Complete the parsing part of the SableCC specifications for MiniJava grammar.
Use the MiniJava files from Homework 2 lexical analysis as a starting point.
path=%path%;\javacc-3.2\bin
IUS:
v:\common\user\C431\CC
- Download Grammar3.32.js, Grammar3.34.js, Main.java and Parse1, Parse2, Parse3.
- To generate the SableCC parser:
Home: java -jar \sablecc-2.18.2\lib\sablecc.jar Grammar3.32.js
IUS: java -jar v:\common\user\C431\sablecc.jar Grammar3.32.js
- Copy Main.java to sub-directory Program3.
- To delete an older version of the parser and compile the Java program named Program3\Main.java enter:
del Program3\Main.class
javac -classpath . Program3\Main.java
- To perform the parsing of Parse1:
java -cp . Program3.Main < Parse1
Basic SableCC Grammar
could be written as:
Should not be necessary for this assignment.
Tokens add = '+'; sub = '-'; mul = '*'; div = '/'; left_paren = '('; right_paren = ')'; number = [0-9]+; whitespace = (' ')+; Ignored Tokens whitespace; Productions expr = {add} [left]:expr add [right]:factor | {sub} [left]:expr sub [right]:factor | {factor} factor; factor= {mul} [left]:factor mul [right]:value | {div} [left]:factor div [right]:value | {value} value; value = {number} number | {parens} left_paren expr right_paren; exprs = expr*;
Complete Example
The following is an example using the StraightLine grammar that includes the SableCC grammar, a Java program to call the parser and a test StraightLine program.
StaightLine.js
Package StraightLine;
Tokens
semi = ';';
lpar = '(';
rpar = ')';
comma = ',';
plus = '+';
minus = '-';
times = '*';
div = '/';
assign = ':=';
print = 'print';
whitespace = (' ' | '\t' | 13 10 | 10 | 13 )+; // 13 10 is '\r' '\n'
num = ['0'..'9']+;
id = ['a'..'z'] (['a'..'z'] | ['0'..'9'])*;
Ignored Tokens
whitespace;
Productions
program = stm;
stm = {compoundstm} onestm stmtail*;
onestm ={assignstm} id assign exp |
{printstm} print lpar explist rpar;
stmtail=semi onestm;
exp = {plusexp} exp plus term |
{minusexp} exp minus term |
{term} term |
{eseqexp} lpar stm comma exp rpar;
term = {timesterm} term times factor |
{divterm} term div factor |
{factor} factor;
factor= {idexp} id |
{numexp} num;
explist={pairexplist} exp comma explist |
{lastexplist} exp;
Main.java
package StraightLine;
import StraightLine.lexer.*;
import StraightLine.node.*;
import StraightLine.parser.*;
import java.io.*;
public class Main{
public static void main(String[] arguments){
try{
Parser parser = new Parser(
new Lexer(
new PushbackReader(
new InputStreamReader(System.in), 1024)));
Start ast = parser.parse();
}
catch(Exception e){
System.out.println("Error: " + e.getMessage());
}
}
}Test
a := 5 + 3 * 4; b:=(print(a,a-1),10*a);
print(b)
Hints