1. An Extended Expression Grammar
Extend the expression grammar from class to include unary operations, exponentiation, and function calls.
We begin with the grammar from class.
Exp ::= Exp AddOp Term
| Term
Term ::= Term MulOp Factor
| Factor
Factor ::= id
| num
| '(' Exp ')'
In order to extend this grammar, we need to consider the relative
precedence of the operations. Exponentiation has higher precedence than
addition or multiplication. For example, A*B^C should be
interpreted as A*(B^C) and not (A*B)^c. Unary
operations have even higher precedence. For example, we want to treat
-3^4 as (-3)^4 and not -(3^4).
[However, it is perfectly acceptable if you make the opposite decision
as long as unary operations have greater precedence than
multiplication. -3*4 is clearly intended to represent
(-3)*4.]
Another issue we need to consider is the effect of multiple copies of the
operation. Exponentiation is right associative rather than left
associative. That is, we treat 2^3^4 as 2^(3^4)
and not (2^3)^4. In general, we don't allow multiple unary
operators. For example, +-x is somewhat meaningless.
Now that we've done some initial analysis, we're ready to build our grammar. We'll add two new nonterminals,
Exponent for something that can be an exponent.
Primary, to represent the most basic things (numbers, identifiers,
parenthesized operations); similar to what Factor was before.
We'll then use Factor to include unary operations.
Exp ::= Exp AddOp Term
| Term
Term ::= Term MulOp Exponent
| Exponent
Exponent ::= Factor '^' Exponent
| Factor
Factor ::= UnOp Primary
| Primary
Primary ::= id
| num
| '(' Exp ')'
Finally, we need to consider how to add functions. A function is just an
identifier followed by an open parenthesis, a list of Expressions,
and a close parenthesis. We'll put functions down at the "base level", along
with identifiers, numbers, and such.
Primary ::= id '(' IdList ')'
IdList ::= empty
| NonemptyIdList
NonemptyIdList ::= id
| id ',' NonemptyIdList
2. Parsing Expressions
Draw the parse trees given by your grammar for the following expressions
(or indicate that the expression is not parsable).
-((id))
id(id+id-id*id)
id() + id() * id()
(num - (num - (num - num)))
This part of the assignment was mostly intended to help you think about the grammar and what should be in it. Here are some sample parses (in a simpler representation of parse trees). Let me know if you can't understand my structure.
-((id))
Exp
Term
Exponent
Factor
Unop
| -
Primary
(
Exp
| Term
| Exponent
| Factor
| Primary
| (
| Exp
| | Term
| | Exponent
| | Factor
| | Primary
| | id
| )
)
3. Syntax of for
Write a BNF description of the Pascal for statement.
ForLoop ::= 'for' id ':=' Exp Direction Exp 'do' Statement Direction ::= 'to' | 'downto'
Note that I didn't have to describe Exp or
Statement as they are likely to be descibed
elsewhere. In addition, I haven't included a semicolon because
not all Pascal statements end with semicolons (in Pascal, semicolons
act as separators, rather than terminators).
By the way, the appropriate reference manual for Pascal is the Pascal User Manual and Report, Second Edition by Kathleen Jensen and Niklaus Wirth from Springer-Verlag. It's also okay to use Standard Pascal User Reference Manual by Doug Cooper.
4. Regularizing Identifiers
Write a regular expression for identifiers in Pascal.
(a|b|...|z|A|B|...|Z)(a|b|...|z|A|B|...|Z|0|1|...|9)*
Some of you worried about what to do about keywords. For now, the correct/reasonable answer is "nothing".
5. A Grammar for Identifiers
Write a BNF description of Pascal identifiers. You can assume that your
lexer has already converted individual characters into digits
and letters.
Identifier ::= letter Additional
Additional ::= empty
| digit Additional
| letter Additional
Wirth uses a more concise notation.
Identifier ::= Letter { LetterOrDigit }
LetterOrDigit ::= Letter | Digit
6. Identifying Identifiers
Pick two "interesting" identifiers and show the parse trees that you get for those identifiers.
No identifiers are particularly intersting in this grammar. All have
more or less identical parse trees. For example, the parse tree for
alpha (or, more precisely,
letter letter letter letter letter) is
Identifier
letter
Additional
letter
Additional
letter
Additional
letter
Additional
letter
Additional
7. A "Functional Grammar" for Identifiers
[Based on the notes of John Stone.]
In your favorite programming language, write and test a Boolean function that takes a string argument and determines whether that string is a legal instance of the syntactic category "identifier" in Pascal. Make sure to turn in an illustration of the results of your test, as well as your function code.
Nope, I'm not writing code. You folks are the coding enthusiasts.
8. A "Functional Grammar" for Numbers
[Based on the note of John Stone.]
In your favorite programming language, write and test a Boolean function that takes a string argument and determines whether that string is a legal instance of the syntactic category "number" in Pascal.
Nope, I'm not writing code. You folks are the coding enthusiasts.
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 Sun Feb 22 19:58:16 1998.
This page generated on Sun Feb 22 20:54:52 1998 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu