This page is an accumulation of (we hope) helpful notes, hints, etc., that might be useful to you in doing Project #1.
program
node.
Thus, what appears in a program as "Input file: C:\\FOO\40contains\t\"Hello, w\137rld!\"\n" gets written out as "Input file: C:\134FOO contains\011\042Hello, world!\042\012"That second line should read
"Input file: C:\\FOO\40contains\t\"Hello, w\157rld!\"\n"
+=
) to be simple variables.
x += E
are now just syntactic
sugar for x = x + E
. They don't use the special functions
such as __iadd
.
for
statement must be a single
simple variable.
getIndentation
function.
parser.y
and Parser.tab.cc
, which we removed (the parser file is
Parser.y
; Parser.tab.cc
is a generated file, not
part of the original source.
There are several erroneous cases that could be handled either in the parser or in static semantics. To allow us to use test cases properly, we have to decide how to handle them. Detection of the following errors can wait until static semantic analysis. You are free to check for them during parsing, but be warned: we are postponing them because they present various difficulties for our grammar tools and yet are easy to handle during static semantics.
Be sure that your finished parser does not report unresolved shift/reduce or reduce/reduce errors. These generally mean you've done something wrong.
With the -v switch, bison and jbison produce a table (in a file with suffix .output) showing the LALR(1) machine that they produce. You can use this as shown in lecture to help find some problems.
We have set up the ParseTest commands in the skeletons to allow a --debug switch, which will cause the parser to output information about the parse it is performing. This information includes the shifts and reductions it performs and the tokens it gets from the lexer.
It's also possible to test the lexer alone. Putting the declaration
%debugfor JFlex or
%option debugfor flex will cause the lexer to output each rule that it matches and value that it returns. Unfortunately, the returned syntactic categories are integers, which are, perhaps, not the most useful things to look at, and the information returned does not include the accompanying lexemes themselves.
So a better a approach to testing the lexer is to set up a dummy version of
the parser, containing %term
declarations for all tokens you
expect from the lexer (the lexer needs the parser's values for these anyway):
%termand a dummy grammar that uses all of them:IDENT STRING_LITERAL INDENT ... %term "and" "or" "if" ... %term "==" "!=" ...
prog : /* EMPTY */ | prog token { System.out.printf ("Token value: %s%n", $2); } ; token : IDENT | STRING_LITERAL | INDENT | ... ;
Is the welter of resulting output hard to read? Consider writing a tiny program to format it better!
Q: How should we treat "elif"? It isn't in the spec.
A: There is no construct for it, because elif is the same as else if:
if FOO: S1 elif BAR: S2 else: S3is equivalent to
if FOO: S1 else: if BAR: S2 else: S3
Q: Are we free to allow (or disallow) statements such as:
x *= y *= 2
A: Augmented assignments are STATEMENTS, not expressions, and may not be used as expressions. Thus the statement above is illegal.
Q: In dictionaries can keys and values only be expressions? If not what syntactic categories can they be?
A: They may only be expressions.
[CS164 Homework]   [CS164 Home Page]
Page was last modified on Thu Feb 28 03:26:06 2008.