[Instructions] [Search] [Current] [News] [Syllabus] [Handouts] [Outlines] [Assignments]
Held Wednesday, September 16, 1998
Notes
parse
function for each nonterminal.
parse function for a program
as
-- Find whether t represents a program. Returns true if it does
-- and false otherwise. Modifies the token stream.
boolean parseProgram(TokenStream t) {
-- Every program must begin with the program keyword
if (t.first() != 'program') then return false;
-- Consume the program token
t.deleteFirst();
-- Check and consume the program name
if (t.first() != identifier) then return false;
t.deleteFirst();
-- Check and consume the left paren
if (t.first() != lparen) then return false;
t.deleteFirst();
-- Get the identifier list
if (!parseIDList(t)) then return false;
-- Check and consume the right paren
if (t.first() != lparen) then return false;
t.deleteFirst();
-- The declaration list
if (!parseDecList(t)) then return false;
-- And the compound statement
if (!parseCompound(t)) then return false;
-- The period and we're done
if (t.first() != period) then return false;
t.deleteFirst();
return true;
end parseProgram
Exp ::= number
Exp ::= identifier
Exp ::= UnOp Exp
UnOp ::= '+' UnOp ::= '-'
Exp := Exp BinOp Exp
BinOp ::= '+' | '-' | '*' | '/'
Exp ::= '(' Exp ')'
a+b*2 is an expression? First, we observe that the
lexer converts this to identifier + identifier * number
Exp => // Exp ::= Exp BinOp Exp Exp BinOp Exp => // Exp ::= identifier identifier BinOp Exp => // BinOp ::= '+' identifier + Exp => // Exp ::= Exp BinOp Exp identifier + Exp BinOp Exp => // Exp ::= number identifier + Exp BinOp number => // Exp ::= identifier identifier + identifier BinOp number => identifier + identifier * number
Exp rather than the
first in Exp BinOp Exp.
Exp
/ | \
/ | \
Exp BinOp Exp------
/ | | | \
/ | | \ \
identifier + Exp BinOp Exp
/ | |
/ | |
identifier * number
Back to Introduction to grammars and parsing. On to Predictive parsing.
[Instructions] [Search] [Current] [News] [Syllabus] [Handouts] [Outlines] [Assignments]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Fri Sep 18 10:52:55 1998.
This page generated on Fri Sep 18 11:46:46 1998 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu