CS374: Programming Language Principles - Grammars
Activity Goals
The goals of this activity are:- To describe a grammar in terms of tokens in BNF form
- To disambiguate a grammar
Supplemental Reading
Feel free to visit these resources for supplemental background reading material.The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity on the Class Activity Questions discussion board. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.Model 1: Chomsky Hierarchy of Languages
Grammar | Languages | Automaton | Production rules (constraints)* | Examples |
---|---|---|---|---|
Type-0 | Recursively enumerable | Turing machine | (no constraints) |
describes a terminating Turing machine |
Type-1 | Context-sensitive | Linear-bounded non-deterministic Turing machine | ||
Type-2 | Context-free | Non-deterministic pushdown automaton | ||
Type-3 | Regular | Finite state automaton | and |
Questions
- What kind of language and machine would be used to balance parenthesis, and why?
- Why can't the context-free language be expressed with a regular language or finite state machine?
- What do you think is added to a finite state machine to create a push-down automata?
Model 2: Grammar Definition
Questions
- What does this language produce?
- How can this be modified to specify a language that accepts
String
s ofn
a characters followed byn
b characters? - How might we parse a grammar? How can we make it easy to parse a grammar? For example, would it help if every production began with a unique terminal?
Model 3: Ambiguous Grammars
Questions
- How might
selection-statement
lead to a dangling else block ambiguity in a nested if statement? - How is the ambiguity resolved using the
open_statement
instead?
Model 4: An Example Grammar with an Ambiguity
Questions
- What are two different ways in which the expression
8*9+5
can be parsed into a parse tree? What does each parse tree look like? - Can the resulting computed values be different as a result of this ambiguity?
- How can we resolve this ambiguity?
Model 5: An Example Grammar without an Ambiguity
Questions
- How would the expression
8*9+5
be parsed into a parse tree? How about5+8*9
? - Given an ambiguous grammar, is there an algorithm that can disambiguate it, or do we have to design against the ambiguity up front? Why or why not?