Presentation is loading. Please wait.

Presentation is loading. Please wait.

Formal grammars A formal grammar is a system for defining the syntax of a language by specifying sequences of symbols or sentences that are considered.

Similar presentations


Presentation on theme: "Formal grammars A formal grammar is a system for defining the syntax of a language by specifying sequences of symbols or sentences that are considered."— Presentation transcript:

1 Formal grammars A formal grammar is a system for defining the syntax of a language by specifying sequences of symbols or sentences that are considered grammatical. Grammatical sentences of a language may be very large or infinite, therefore they are usually derived by a recursive definition.

2 Definition of the formal grammar G G = V – set of terminal symbols Σ – set of nonterminal symbols with the restriction that V and Σ are disjoint σ – start symbol P – set of production rules in a form: A –> B where: A – is a sequence of symbols having at least one nonterminal, B – is the result of replacing some nonterminal symbol A with a sequence of symbols (possibly empty) from V and Σ

3 Small subset of English grammar V = {“the”, ”a”, ”cat”, ”dog”, ”saw”, “chased“}  = {S, NP, VP, D, N, V} S – sentenceD – determiner NP – noun phraseN – noun VP – verb phraseV – verb  = S P = { S –> NP VP, NP –> D N, VP –> V NP, D –> ”the”, D –> “a”, N –> ”cat”, N –> ”dog”, V –> “saw”,V –> “chased” }

4 Derivation Example of a leftmost derivation: S –>NP VP –>D N VP –>“the” N VP –>“the” “cat” VP –>“the” “cat” V NP –>“the” “cat” “chased” NP –>“the” “cat” “chased” D N –>“the” “cat” “chased” “a” N –>“the” “cat” “chased” “a” “dog”

5 S NP DV “the” N “cat” VP “chased”D “a” N “dog” Parse trees

6 Backus notation for production rules ::=– is defined as |– separates alternatives <>– denotes nonterminal symbols Production rules for the small subset of English grammar P = { ::=, ::= ”the” | “a”, ::= ”cat” | ”dog”, ::= “saw” | “chased” }

7 Classification of formal grammars TypeNameProduction rulesRecognizing automaton / Storage required / Parsing complexity 3Regular grammars, Finite state grammars A –> xB C –> y A, B, C – non-terminal symbols x, y – terminal symbols Finite state automaton / Finite storage / O (n) 2Context free grammars A –> BC…D A – non-terminal symbols BC…D – any sequence of terminal or non-terminal symbols Pushdown automaton / Pushdown stack / O (n 3 ) 1Context sensitive grammars aAz –> aBC…Dz A – non-terminal symbols a, z – sequences of zero or more terminal or non-terminal symbols BC…D – any sequence of terminal or non-terminal symbols Linear bounded automaton (non-deterministic Turing machine) / Tape being a linear multiple of input length / NP Complete 0Unrestricted grammars, General rewrite grammars Allows the production rules to transform any sequence of symbols into any other sequence of symbols. To convert context-sensitive grammar into unrestricted grammar, replacement of any non-terminal symbol A with an empty sequence needs to be allowed. Turing machine / Infinite tape / Undecidable

8 Classification of formal grammars Regular grammars Context free grammars Context sensitive grammars Unrestricted grammars Generalization / difficulty of parsing

9 Parsing methods Top-down parsing approach: LL parsers – Left to Right, Leftmost Derivation Bottom-up parsing approach LR parsers – Left to Right, Rightmost Derivation LALR parsers –Look Ahead LR (use of lookahead symbols to aid the parsing process) GLR parsers –Generalized LR (multiple parsing threads in order to resolve ambiguities)

10 Grammar of Simple Arithmetic Expressions V = {“a”, ”b”, ”d”, ”+”, ”*”, “(“, “)”}  = {E, C, F} E – expression C – component F – factor  = E P = { ::= | “+” | “+” ::= | “*” | “*” ::= “(“ “)” | “a” | “b” | “d” }

11 Grammar of Reverse Polish Notation V = {“a”, ”b”, ”d”, ”+”, ”*”}  = {W, Z, X, O}  = W P = { ::= | ::= “a” | “b” | “d” ::= “+” | “*” }

12 (a + b) * d  a b + d *  d a b + * “a”“b”“+” “*” “d”

13 Algorithm of finding the result of arithmetic expressions in RPN START Parameter No Yes Read a symbol (from the left to the right) Put the parameter on the top of the stack Operator Yes Get parameters from the stack, execute the operation, put the result on the top of the stack  ENDERROR No Yes 2, 3, 4, 5, +, *, +, InputStack 22 32, 3 42, 3, 4 52, 3, 4, 5 +2, 3, 9 *2, 27 +29 


Download ppt "Formal grammars A formal grammar is a system for defining the syntax of a language by specifying sequences of symbols or sentences that are considered."

Similar presentations


Ads by Google