Alternative formulations of the LL1 conditions
9.2.4 Alternative formulations of the LL1 conditions
The two rules for determining whether a grammar is LL1 are sometimes found stated in other ways which are, of course, equivalent. Some authors combine them as follows: Combined LL1 Rule A grammar is LL1 if for every non-terminal A that admits alternatives A 1 | 2 | . . . n the following holds FIRST j ° FOLLOWA FIRST k ° FOLLOWA = Ø, j k where ° denotes composition in the mathematical sense. Here the cases j and j are combined - for j we have that FIRST j ° FOLLOWA = FIRST j , while for j we have similarly that FIRST j ° FOLLOWA = FOLLOWA . Other authors conduct this discussion in terms of the concept of director sets. For every non-terminal A that admits to alternative productions of the form A 1 | 2 | . . . | n we define DSA, k for each alternative to be the set which helps choose whether to use the alternative; when the input string contains the terminal a we choose k such that a DSA, k . The LL1 condition is then DSA, j DSA, k = Ø, j k The director sets are found from the relation a DSA, k if either a FIRST k if k or a FOLLOWA if k Exercises 9.1 Test the following grammar for being LL1 G = { N , T , S , P } N = { A , B } T = { w , x , y , z } S = A P = A B x | z | w | z B B x B z | { y } 9.2 Show that the grammar describing EBNF itself section 5.9.1 is LL1. 9.3 The grammar for EBNF as presented in section 5.9.1 does not allow an implicit to appear in a production, although the discussion in that section implied that this was often found in practice. What change could you make to the grammar to allow an implicit ? Is your resulting grammar still LL1? If not, can you find a formulation that is LL1? 9.4 In section 8.7.2, constant declarations in Clang were described by the productions ConstDeclarations = CONST OneConst { OneConst } . OneConst = identifier = number ; . Is this part of the grammar LL1? What would be the effect if one were to factorize the grammar ConstDeclarations = CONST OneConst { ; OneConst } ; . OneConst = identifier = number . 9.5 As a more interesting example of applying an analysis to a grammar expressed in EBNF, let us consider how we might describe the theatrical production of a Shakespearian play with five acts. In each act there may be several scenes, and in each scene appear one or more actors, who gesticulate and make speeches to one another for the benefit of the audience, of course. Actors come onto the stage at the start of each scene, and come and go as the scene proceeds - to all intents and purposes between speeches - finally leaving at the end of the scene in the Tragedies some may leave dead, but even these usually revive themselves in time to go home. Plays are usually staged with an interval between the third and fourth acts. Actions like speech, entry and exit are really in the category of the lexical terminals which a scanner in the person of a member of the audience would recognize as key symbols while watching a play. So one description of such a staged play might be on the lines of Play = Act Act Act interval Act Act . Act = Scene { Scene } . Scene = { speech } entry { Action } . Action = speech | entry | exit | death | gesticulation . This does not require all the actors to leave at the end of any scene sometimes this does not happen in real life, either. We could try to get this effect by writing Scene = { speech } entry { Action } { exit } . but note that this context-free grammar cannot force as many actors to leave as entered - in computer language terms the reader should recognize this as the same problem as being unable to specify that the number of formal and actual parameters to a procedure agree. Analyse this grammar in detail. If it proves out to be non-LL1, try to find an equivalent that is LL1, or argue why this should be impossible.9.3 The effect of the LL1 conditions on language design
Parts
» compilers and compiler generators 1996
» Objectives compilers and compiler generators 1996
» Systems programs and translators
» The relationship between high-level languages and translators
» T-diagrams Classes of translator
» Phases in translation compilers and compiler generators 1996
» Multi-stage translators compilers and compiler generators 1996
» Interpreters, interpretive compilers, and emulators
» Using a high-level host language Porting a high level translator
» Bootstrapping Self-compiling compilers compilers and compiler generators 1996
» The half bootstrap compilers and compiler generators 1996
» Bootstrapping from a portable interpretive compiler A P-code assembler
» Simple machine architecture compilers and compiler generators 1996
» Addressing modes compilers and compiler generators 1996
» Machine architecture Case study 1 - A single-accumulator machine
» Instruction set Case study 1 - A single-accumulator machine
» A specimen program Case study 1 - A single-accumulator machine
» An emulator for the single-accumulator machine
» A minimal assembler for the machine
» Machine architecture Case study 2 - a stack-oriented computer
» Instruction set Case study 2 - a stack-oriented computer
» Specimen programs Case study 2 - a stack-oriented computer
» An emulator for the stack machine
» Syntax, semantics, and pragmatics
» Languages, symbols, alphabets and strings Regular expressions
» Grammars and productions compilers and compiler generators 1996
» Classic BNF notation for productions
» Simple examples compilers and compiler generators 1996
» Phrase structure and lexical structure -productions
» Semantic overtones The British Standard for EBNF Lexical and phrase structure emphasis
» One- and two-pass assemblers, and symbol tables
» Source handling Towards the construction of an assembler
» Lexical analysis Towards the construction of an assembler
» Syntax analysis Towards the construction of an assembler
» Symbol table structures The first pass - static semantic analysis
» The second pass - code generation
» Symbol table structures The first pass - analysis and code generation
» Error detection compilers and compiler generators 1996
» Simple expressions as addresses
» Improved symbol table handling - hash tables
» Macro processing facilities compilers and compiler generators 1996
» Conditional assembly compilers and compiler generators 1996
» Relocatable code compilers and compiler generators 1996
» Further projects compilers and compiler generators 1996
» Equivalent grammars Case study - equivalent grammars for describing expressions
» Useless productions and reduced grammars -free grammars
» Ambiguous grammars compilers and compiler generators 1996
» Type 1 Grammars Context-sensitive The Chomsky hierarchy
» The relationship between grammar type and language type
» EBNF description of Clang A sample program
» Context sensitivity Deterministic top-down parsing
» Terminal successors, the FOLLOW function, and LL1 conditions for non -free grammars
» Further observations Restrictions on grammars so as to allow LL1 parsing
» Alternative formulations of the LL1 conditions
» The effect of the LL1 conditions on language design
» Construction of simple recursive descent parsers
» Case studies compilers and compiler generators 1996
» Syntax error detection and recovery
» Construction of simple scanners
» LR parsing compilers and compiler generators 1996
» Automated construction of scanners and parsers
» Embedding semantic actions into syntax rules
» Attribute grammars compilers and compiler generators 1996
» Synthesized and inherited attributes
» Classes of attribute grammars
» Case study - a small student database
» Installation Input file preparation
» Execution Output from CocoR Assembling the generated system
» Case study - a simple adding machine
» Character sets Comments and ignorable characters
» Pragmas User names The scanner interface
» Syntax error recovery Parser specification
» Grammar checks Semantic errors
» Interfacing to support modules The parser interface
» Essentials of the driver program Customizing the driver frame file
» Case study - Understanding C declarations
» Case study - Generating one-address code from expressions
» Case study - Generating one-address code from an AST
» Case study - How do parser generators work?
» Project suggestions compilers and compiler generators 1996
» Overall compiler structure compilers and compiler generators 1996
» A hand-crafted source handler
» A hand-crafted scanner Lexical analysis
» A CocoR generated scanner Efficient keyword recognition
» A hand-crafted parser A CocoR generated parser
» Syntax error handling in hand-crafted parsers
» Error reporting The symbol table handler
» Other aspects of symbol table management - further types
» The code generation interface
» Code generation for a simple stack machine
» Machine tyranny Other aspects of code generation
» Abstract syntax trees as intermediate representations
» Simple optimizations - constant folding
» Simple optimizations - removal of redundant code
» Generation of assembler code
» Source handling, lexical analysis and error reporting Syntax
» The scope and extent of identifiers
» Symbol table support for the scope rules
» Parsing declarations and procedure calls
» Hypothetical machine support for the static link model
» Code generation for the static link model
» Hypothetical machine support for the display model
» Code generation for the display model
» Relative merits of the static link and display models
» Syntax and semantics compilers and compiler generators 1996
» Symbol table support for context-sensitive features
» Actual parameters and stack frames
» Hypothetical stack machine support for parameter passing
» Semantic analysis and code generation in a hand-crafted compiler
» Semantic analysis and code generation in a CocoR generated compiler
» Scope rules Language design issues
» Function return mechanisms Language design issues
» Context sensitivity and LL1 conflict resolution Fundamental concepts
» Parallel processes, exclusion and synchronization
Show more