Syntax Semantics - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Syntax Semantics

Description:

ITK 327. 1. Syntax -- Semantics. Grammar. Determines. Determines ? ... I saw a girl with her boyfriend. I saw a pretty girl with a telescope on the mountain top. ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 28
Provided by: ChungC7
Category:
Tags: girl | semantics | syntax

less

Transcript and Presenter's Notes

Title: Syntax Semantics


1
Syntax -- Semantics
Grammar
Determines ??
Help??
Determines
Ambiguities
2
Ambiguity in English
I saw an airplane with a telescope.
I saw a girl with her boyfriend.
I saw a scout with a telescope on the mountain
top.
I saw a pretty girl with a telescope on the
mountain top.
3
Ambiguity in Programming Languages
if (a 2) if (b 1) b else
a
Dangling else
if (a 2) if (b 1) b else a
4
Three Equivalent Grammars
G1 - a b c
G2 -
a b c
G3 -
a b c
They are equivalent! (what does that mean?)
5
Parse a-b-c using G1
G1 - a b c


-


-


a
-


-


c
b
c
a
b
G1 is an ambiguous grammar
6
Left and Right Association rules
Right associative rule a-b-c-d a (b-(c-d))
Left associative rule a-b-c-d ((ab)-c)-d
7
G2 and G3 Grammars
G2 -
a b c G3
- a b c
Right associative rule
Left associative rule a-b-c-d ((ab)-c)-d
8
A grammar for Arithmetic Expressions

( ) a
b c
Example ((ab)c)
9
What is the meaning of abc




c

a

b
c
a
b
10
An unambiguous grammar for Arithmetic Expressions


( )
a b c




a
Example abc

b
c
11
An unambiguous grammar for Arithmetic Expressions


( )
a b c




a

Example abc

Right association
b
abc a(bc)
c
12
Alter the order


( )
a b c




( )
Example (ab)c
c

a
b
13
An unambiguous left associative grammar for
Arithmetic Exp.
No such thing called left associative grammar

Better way to say this A grammar for left
associative arithmetic exp.


( )
a b c




c
Example abc

b
a
14
Dangling else
s1 s2 if
then else if
then e1 e2
if e1 then if e2 then s1 else s2
if (a 2) if (b 1) b else
a
if (a 2) if (b 1) b else a
15
if (a 2) if (b 1) b else a
if (a 2) if (b 1) b else
a
Most languages else goes withnearest then
16
Eliminating The Ambiguity
s1 s2 if
then else
if then e1 e2
then-else are all matched
s1 s2
if then else
17
A Parse Tree for if-stmt
18
Languages That Dont Dangle
  • Algol then part cant be another if (a block is
    allowed)
  • Ada if statement must be terminated with an end
    if

19
Options
  • Rewrite grammar to eliminate ambiguity
  • Leave ambiguity but explain in accompanying text
    how things like associativity, precedence, and
    the dangling else should be parsed
  • Do both in separate grammars

20
Abstract Syntax Tree
  • Abbreviated version of the parse tree
  • Details are implementation-dependent
  • Usually, there is a node for every operation,
    with a subtree for every operand

Most systems construct an AST instead
21
parse tree
abstract syntax tree
22
Operators
  • Special symbols for frequently-used simple
    operations like addition, subtraction,
    multiplication and division
  • Usually predefined, but not always
  • Usually a single token, but not always

23
Operands
  • Operands are the inputs to an operator, like 1
    and 2 in the expression 12
  • Unary operators take one operand -1
  • Binary operators take two 12
  • Ternary operators take three a?bc

24
Operator Position
  • infix a b
  • prefix a b
  • postfix a b

25
Operator Precedence
  • Most languages put at a higher precedence level
    than .
  • abc a(bc)

(sign, as unary operators)
-

/
-

26
Precedence Examples in C
a b c
a b a 27
Some conclusions from the textbook
with (my view)
  • Grammars define syntax, and more (more?)
  • They define not just a set of legal programs, but
    the (a) parse tree for each program
  • The structure of a parse tree corresponds to the
    order (entity) in which different parts of the
    program are to be executed (as an entity)
  • Thus, grammars contribute (a little) to the
    definition of semantics
Write a Comment
User Comments (0)
About PowerShow.com