CPSC 460/560
Project Part 2: mingrammar, minyacc.y, minlex.l
The Parser
Syntactical Analysis
This is the second part of the compiler development project that we are
doing this semester. Recall, you are building a compiler for the
minl programming language.
Minl is a minimal
programming language containing just enough high level features to
write useful code. (Remember that this is an approach to building a
compiler for a full version of a language.)
The first part of the project had you construct the lexical analyzer
using the Lex tool.
This part of the assignment requires you to analyze the syntactical
correctness of the input using the Yacc (yet another compiler
compiler) tool.
Specifically, you are to write a Yacc specification for the grammar of
the minl language. The details of the language features are
given as a set of separate syntax diagrams. A syntax diagram
is a directed graph with one entry and one exit. Each path through
the graph defines an allowable sequence of symbols. Rounded boxes
denote the tokens seen in the input (as returned by the scanner).
Rectangular boxes denote the names of other syntax diagrams. Some
additional details of the language include:
- The operators $+, -$ and $*$ are left associative.
- Multiplication (the $*$ operator) has precedence over the $+$ and
$-$ operators.
Approach
First, you should develop a context-free grammar for the minl
language specified by the syntax diagrams. Then, you should specify
your grammar in Yacc. When you execute yacc to build a parser for the
grammar you specified, you must be sure that it reports no conflicts;
otherwise, you will need to modify the grammar.
To help you convert this later to yacc, use the following rules:
instead of writing:
expr -> expr + term | term
term -> DIGIT
use yacc format:
expr : expr + term
| term
;
term : DIGIT
;
Hand this in (typed, so you will have it for your yacc source)
by Wednesday, Monday March 26. This will count as a homework.
yacc
use of yacc is similar to lex. Your book gives a simple user's guide
on pages 287-297 (the subsection is entitled ``The Parser Generator
Yacc''). I have typed figure 4.59 on a web page so you
can practice. You can compile this program, then type in expressions
such as: 4+5, and the program will tell you 9 (remember, no spaces are
allowed and you may only use single digits).
To Hand In
First the grammar.
Type this into a text file named grammar.
Next, turn in the yacc program:
all the code needed to build a fully-functional
parser. Note that this necessarily includes a scanner (which you've
already built, but it may need to be modified a little).
This should be contained in two files: minyacc.y
and minlex.l
The parser for this assignment should output: 1) An accepted or
not accepted answer depending upon whether or not the input
matches the grammar specifications or not; and, 2) The series of
grammar rules applied for accepting the input.
Diagram 1
and
Diagram 2
Example Input
program myprog;
int v;
begin
v = 3;
end
Example Output
VARDECLS -> v
DECL -> int VARDECLS
DECLIST -> DECL ;
TERM -> 3
EXPR -> TERM
ASGNSTMT -> v = EXPR
STMT -> ASGNSTMT
STMTLIST -> STMT ;
start -> program myprog ; DECLIST begin STMTLIST end
Program myprog accepted
To hand in
You will need two files, your lexical analyzer
and your parser.