Syntax and Symantic Presentation
Syntax and Symantic Presentation
Introduction
Providing a precise description of a programming
language is important.
Reasons:
-Diversity of the people who need to understand
-Language implementors must determine how the
expressions, statements, etc are formed, and their
intended effects – clear description of language make their
job easy
-Language users must understand the language by
referring to the language manual
Introduction
Syntax of a PL: the form of its expressions,
statements, and program units.
Lexemes
Tokens
Describing Syntax
Elements of Syntax
• An alphabet of symbols
• Symbols are terminal and non-terminal
– Terminals cannot be broken down
– Non-terminals can be broken down further
• Grammar rules that express how symbols are combined to
make legal sentences
• Rules are of the general form
non-terminal symbol ::= list of zero or more terminals or non-
terminals
• One uses rules to recognize (parse) or generate legal
sentences
Recognizers vs Generators
Fundamentals
• <LHS> → <RHS>
• LHS: abstraction being defined
• RHS: definition
Fundamentals
Fundementals
*Alternation
• Multiple definitions can be separated by | to mean OR.
Alternatively
<sentence> ::= <subject><predicate>
<subject> ::= noun
<predicate> ::= <verb><object>
<verb> ::= greets
<object> ::= <noun>
<noun> ::= John | Mary
CS315 Programming Languages © Pinar Duygulu
17
*All numbers
• S := '-' FN | FN
• FN := DL | DL '.' DL
• DL := D | D DL
• D := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
*Identifiers
<identifier> → <letter>
| <identifier><letter>
| <identifier><digit>
PASCAL/Ada If Statement
or
Example grammar
Derivations
Example
<program> ⇒ begin <stmt_list> end
⇒ begin <stmt> ; <stmt_list> end
⇒ begin <var> := <expression>; <stmt_list> end
⇒ begin A := <expression>; <stmt_list> end
⇒ begin A := B; <stmt_list> end
Each of
these strings ⇒ begin A := B; <stmt> end
is called
sentential
⇒ begin A := B; <var> := <expression> end
form ⇒ begin A := B; C := <expression> end
⇒ begin A := B; C := <var><arith_op><var> end
⇒ begin A := B; C := A <arith_op> <var> end
⇒ begin A := B; C := A * <var> end
⇒ begin A := B; C := A * B end
Another example
Derivation
A = B * (A + C)
Parse Trees
Parse trees
Ambiguous Grammar
Ambiguity
- Operator precedence
-Associativity rules
Operator precedence
Associativity of Operators
What about equal precedence operators?
Left associativity
Left addition is lower than the right addition
{X1|X2|X3} : choose X1 or X2 or X3
A::=B(X|Y)C is equal to A::=AXC | AYC
<for_stmt> ::= for <var>:=<exp>(to|downto) <exp> do <stmt>
<term>::=<term>(*|/|%)<factor)
CS315 Programming Languages © Pinar Duygulu
45
BNF:
< expr> ::= <expr> + <term>
| <expr> - <term>
| <term>
<term> ::= <term> * <factor>
| <term> / <factor>
| <factor>
<factor> ::= <expr> ** <factor>
| <expr>
<expr> ::= (<expr>)
| <id>
*Extended BNF
*Syntax Graphs
*Example
unmatched
stmt
i logic_ex the
f pr n
matche els unmatche
d e d
A syntax graph consists of one entry and one or more exit points
If there exists a path from the input entry to any of the exit points
corresponding to the string, then the string represents a valid instance
of that unit. There may exist loops in the path