Download presentation
Presentation is loading. Please wait.
Published byVirginia Fox Modified over 9 years ago
1
Specifying Languages CS 480/680 – Comparative Languages
2
Language Specification2 Specifying a Language Informal methods Textbooks, tutorials, etc. Formal definitions Needed for exactness Compiler writers, etc. Like technical specifications for design Syntax – what expressions are legal? Semantics – what should they do?
3
Language Specification3 Context Free Grammars Definition: A context-free grammar (CFG) is a 4-tuple, G = (V, , R, S) V = variables, non-terminal symbols = terminal symbols (alphabet) R = production rules S = start symbol, S V V, , R, S are all finite
4
Language Specification4 A Context Free Grammar V = A, B = (a, b) R =A aAa A B B bBb B A B S = A A aAaA aAa aaAaaA aAa aaBaaA B aabBbaaB bBb aabbBbbaaB bBb aabbbbaB What language does this grammar specify?
5
Language Specification5 Another Example CFG V = A = (a, b) R =A aAa A bAb A a A b A S = A What language does this grammar specify?
6
Language Specification6 More examples Write a CFG for the following languages: “All strings consisting of one or more a’s, followed by twice as many b’s.” “Strings with more a’s than b’s.” There is an entire class devoted to formal specifications of languages: CS 466/666 – Introduction to Formal Languages
7
Language Specification7 A CFG for Integer Arithmetic Expressions V =,,, = [(, ), 0…9, , , , ] R = ( ) | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | | | S =
8
Language Specification8 Derivation of an Expression ( ) ( ) + ( ) + ( ) + ( ) + ( ) + ( ) + (7 ) + (73 ) + (73 ) + (73 4) + (73 4) + (73 4) + (73 4) + 9
9
Language Specification9 Parse Trees The derivation of an expression can also be expressed as a tree This parse tree can help to resolve the interpretation of an expression A compiler reads in the source code, and produces a parse tree before generating code.
10
Language Specification10 Example Parse Tree A simple CFG: E E E | 0 | 1 E E E E E E 1 E E 1 0 E 1 0 1 E E E E E 1 1 0 E E E E E 1 1 0 Since there are two parse trees for this expression, the grammar is ambiguous. (Note: the order of substitution is not the issue.) Since there are two parse trees for this expression, the grammar is ambiguous. (Note: the order of substitution is not the issue.) (1 – 0) – 11 – (0 – 1)
11
Language Specification11 Ambiguity If there are two parse trees for any expression, the grammar is syntactically ambiguous Programming languages should be specified by unambiguous grammars Otherwise it is difficult to determine the semantics of a syntactically correct statement a = b + c * d; Conventions (like operator precedence) can be used to clarify syntactically ambiguous grammars
12
Language Specification12 Disambiguating a grammar We can disambiguate our simple grammar by adding explicit parentheses: E (E E) | 0 | 1 E E E (E E) E (1 E) E (1 0) E (1 0) 1 In general, you can remove ambiguity in a grammar by imposing state in the derivation.
13
Language Specification13 An ambiguous grammar S aSb | aSbb | Language: L = {a n b m | 0 n m 2n} The number of b’s is between the number of a’s and twice the number of a’s aabbb can be generated two ways Disambiguating: Step 1: Produce all a’s with matching b’s Step 2: Produce all extra b’s. S aSb | A | A aAbb | abb
14
Language Specification14 BNF Backus-Naur Form A standard notation for CFG’s, often used in specifying languages Non-terminals (variables) are enclosed in <> , = is the production symbol ( ) | is used for “or”
15
Language Specification15 BNF Example . | Can we generate the number “.7” from this grammar?
16
Language Specification16 Extended BNF Makes some constructs easier to specify No more powerful than BNF Rules: { } = “zero or more” [ ] = “optional” or, equivalently “zero or one” | = “or” ( ) are used for grouping
17
Language Specification17 Arithmetic Expressions ::= + | – | ::= * | / | ::= number | name | | ( ) ::= { (+| – ) } ::= { (*| / ) } ::= ‘(’ ‘)’ | number | name
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.