Consider the context-free grammar:
S -> S S + | S S * | a
and the string aa + a*.
S =lm=> SS* => SS+S* => aS+S* => aa+S* => aa+a*
S =rm=> SS* => Sa* => SS+a* => Sa+a* => aa+a*
Unambiguous
The set of all postfix expressions consist of addition and multiplication
Repeat Exercise 4 . 2 . 1 for each of the following grammars and strings:
S -> 0 S 1 | 0 1 with string 00011l.
S -> + S S | * S S | a with string + * aaa.
! S -> S (S) S | ε with string (()())
! S -> S + S | S S | (S) | S * | a with string (a+a)*a
! S -> (L) | a 以及 L -> L, S | S with string ((a,a),a,(a))
!! S -> a S b S | b S a S | ε with string aabbab
The following grammar for boolean expressions:
bexpr -> bexpr or bterm | bterm
bterm -> bterm and bfactor | bfactor
bfactor -> not bfactor | (bexpr) | true | false
2、
3、
4、
5、
6、
7、 Unambiguous, boolean expression
Design grammars for the following languages:
1、
S -> (0?1)*
2、
S -> 0S0 | 1S1 | 0 | 1 | ε
3、
S -> 0S1S | 1S0S | ε
5、
S -> 1*(0+1?)*
There is an extended grammar notation in common use. In this notation, square and curly braces in production bodies are metasymbols (like -> or |) with the following meanings:
Show that these two extensions do not add power to grammars; that is, any language that can be generated by a grammar with these extensions can be generated by a grammar without the extensions.
extended grammar | not extended grammar |
---|---|
A -> X[Y]Z | A -> XZ | XYZ |
A -> X{YZ} | A -> XB B -> YZB | ε |
Use the braces described in Exercise 4.2.4 to simplify the following grammar for statement blocks and conditional statements:
stmt -> if expr then stmt else stmt
| if stmt them stmt
| begin stmtList end
stmtList -> stmt; stmtList | stmt
stmt -> if expr then stmt [else stmt]
| begin stmtList end
stmtList -> stmt [; stmtList]
Extend the idea of Exercise 4.2.4 to allow any regular expression of grammar symbols in the body of a production. Show that this extension does not allow grammars to define any new languages.
Every regular grammar has a corresponding not extended grammar
A grammar symbol X (terminal or nonterminal) is useless if there is no derivation of the form S =*=> wXy =*=> wxy. That is, X can never appear in the derivation of any sentence.
Give an algorithm to eliminate from a grammar all productions containing useless symbols.
Apply your algorithm to the grammar:
S -> 0 | A
A -> AB
B -> 1
The grammar in Fig. 4.7 generates declarations for a single numerical identifier; these declarations involve four different, independent properties of numbers.
stmt -> declare id optionList
optionList -> optionList option | ε
option -> mode | scale | precision | base
mode -> real | complex
scale -> fixed | floating
precision -> single | double
base -> binary | decimal
Generalize the grammar of Fig. 4.7 by allowing n options Ai, for some fixed n and for i = 1,2... ,n, where Ai can be either ai or bi· Your grammar should use only 0(n) grammar symbols and have a total length of productions that is O(n).
! The grammar of Fig. 4.7 and its generalization in part (a) allow declarations that are contradictory and/or redundant, such as
declare foo real fixed real floating
We could insist that the syntax of the language forbid such declarations; that is, every declaration generated by the grammar has exactly one value for each of the n options. If we do, then for any fixed n there is only a finite number of legal declarations. The language of legal declarations thus has a grammar (and also a regular expression), as any finite language does. The obvious grammar, in which the start symbol has a production for every legal declaration has n! productions and a total production length of O(n x n!). You must do better: a total production length that is O(n2^n)
!! Show that any grammar for part (b) must have a total production length of at least 2n.
What does part (c) say about the feasibility of enforcing nonredundancy and noncontradiction among options in declarations via the syntax of the programming language?
1、
stmt -> declare id optionList
optionList -> optionList option | ε
option -> A_1 | A_2 | … | A_n
A_1 -> a_1 | b_1
A_2 -> a_2 | b_2
…
A_n -> a_n | b_n