Syntax and Semantics of Context-Free Grammar in Programming Languages - Prof. Laura Dillon, Exams of Programming Languages

An introduction to the concepts of syntax and semantics in programming languages, focusing on context-free grammar. It covers the rules and principles that govern sentence structure, nonterminal and terminal symbols, productions, and derivation sequences. The document also discusses the limitations of context-free grammar and the need for other approaches like precedence rules and regular expressions for certain syntax definitions.

Typology: Exams

Pre 2010

Uploaded on 07/28/2009

koofers-user-iud-1
koofers-user-iud-1 🇺🇸

9 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
L. Dillon CSE 452, Fall 2008
1
Syntax
“... the rules and principles that govern the sentence structure
of any individual language” -- Wikipedia
Standard: base a programming language syntax on a context
free grammar
Semantics
the meaning or relationship of meanings of a sign or set of
signs” -- Merriam-Webster Dictionary
Standards for defining semantics vary
Foundations: program definition
2
L. Dillon CSE 452, Fall 2008
2
Nonterminal symbols for
key syntactic categories
A start symbol
(a nonterminal)
Terminal symbols, which
appear in “sentences”
Productions , rules show-
ing how symbols compose
to form sentences
Context free grammar
3
L. Dillon CSE 452, Fall 2008
3
Nonterminal symbols for
key syntactic categories
A start symbol
(a nonterminal)
Terminal symbols, which
appear in “sentences”
Productions , rules show-
ing how symbols compose
to form sentences
Context free grammar
• Example nonterminals:
P - programs # start
S - statements
e - expressions
Example terminals:
0 3.14 15 ‘a’ “hi” ... literals
if then else end... keywords
x y time len ... identifiers
< => + * / ... oper ators
Our examples will use:
num - for numeric literals
id - for identifiers
op - for operators
Context free grammar
4
L. Dillon CSE 452, Fall 2008
4
Nonterminal symbols for
key syntactic categories
A start symbol
(a nonterminal)
Terminal symbols, which
appear in “sentences”
Productions , rules show-
ing how symbols compose
to form sentences
Context free grammar
Example productions
e ::= num
| id
| e op e
| ( e )
S ::= read id
| write e
| id := e
| S ; S
| if e then S else S
| while e do S
| { S }
P ::= prog S end
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Syntax and Semantics of Context-Free Grammar in Programming Languages - Prof. Laura Dillon and more Exams Programming Languages in PDF only on Docsity!

L. Dillon CSE 452, Fall 2008

1

  • Syntax

− “... the rules and principles that govern the sentence structure of any individual language ” -- Wikipedia − Standard:base a programming language syntax on a context free grammar

  • Semantics

− “ the meaning or relationship of meanings of a sign or set of signs ” -- Merriam-Webster Dictionary −Standards for defining semantics vary

Foundations: program definition

L. Dillon CSE 452, Fall 2008

2

- Nonterminal symbols for

key syntactic categories

  • A start symbol

(a nonterminal)

- Terminal symbols, which

appear in “sentences”

- Productions , rules show-

ing how symbols compose

to form sentences

Context free grammar

3

3

- Nonterminal symbols for

key syntactic categories

  • A start symbol

(a nonterminal)

- Terminal symbols, which

appear in “sentences”

- Productions , rules show-

ing how symbols compose

to form sentences

Context free grammar

  • Example nonterminals: P - programs # start S - statements e - expressions

Example terminals:

0 3.14 15 ‘a’ “hi” ... literals if then else end... keywords x y time len ... identifiers < => + * / ... operators

Our examples will use:

num - for numeric literals id - for identifiers op - for operators

Context free grammar

4

4

- Nonterminal symbols for

key syntactic categories

  • A start symbol

(a nonterminal)

- Terminal symbols, which

appear in “sentences”

- Productions , rules show-

ing how symbols compose

to form sentences

Context free grammar

Example productions

e ::= num | id | e op e | ( e ) S ::= read id | write e | id := e | S ; S | if e then S else S | while e do S | { S } P ::= prog S end

L. Dillon CSE 452, Fall 2008

5

A sequence of replacement steps, starting from the start symbol and producing a string of nonterminals

P $ prog S end $ prog S ; S end $ prog read x; S end $ prog read x; if e then S else S end $ prog read x; if e > e then S else S end $ prog read x; if x > e then S else S end $ prog read x; if x > 0 then S else S end $... $ prog read x; if x > 0 then write 0 else write 1 end

Derivation

L. Dillon CSE 452, Fall 2008

6

A sequence of replacement steps, starting from the start symbol and producing a string of nonterminals

P $ prog S end $ prog S ; S end $ prog read x; S end $ prog read x; if e then S else S end $ prog read x; if e > e then S else S end $ prog read x; if x > e then S else S end $ prog read x; if x > 0 then S else S end $... $ prog read x; if x > 0 then write 0 else write 1 end

Derivation

P ::= prog S end

7

7

A sequence of replacement steps, starting from the start symbol and producing a string of nonterminals

P $ prog S end $ prog S ; S end $ prog read x; S end $ prog read x; if e then S else S end $ prog read x; if e > e then S else S end $ prog read x; if x > e then S else S end $ prog read x; if x > 0 then S else S end $... $ prog read x; if x > 0 then write 0 else write 1 end

Derivation

S ::= S ; S

8

8

A sequence of replacement steps, starting from the start symbol and producing a string of nonterminals

P $ prog S end $ prog S ; S end $ prog read x; S end $ prog read x; if e then S else S end $ prog read x; if e > e then S else S end $ prog read x; if x > e then S else S end $ prog read x; if x > 0 then S else S end $... $ prog read x; if x > 0 then write 0 else write 1 end

Derivation

S ::= read id

L. Dillon CSE 452, Fall 2008

13

Some syntax is not expressed in CFG

  • More convenient/efficient not to use CFG for some pur-

poses, e.g.

− Precedence/associativity rules for operators − Regular expressions for “syntax” of classes of terminals, such as integers, floating point numbers, characters, strings, etc.

  • CFG not sufficiently expressive for some purposes, e.g. − Scoping constraints − Typing constraints - Static semantics refers to the syntax of a programming

language that is not expressed in CFG

− contrast this with dynamic semantics , which is generally what we mean by “semantics”

L. Dillon CSE 452, Fall 2008

14

Defining (dynamic) semantics

Much more difficult than defining syntax

  • Typically done informally or, at best, semi-formally
  • Formal approaches have influenced designs of modern program- ming languages −Denotational semantics – HO functions, continuations, ... − Operational semantics – documentation, typing rules, ... − Axiomatic semantics – program assertions, contracts, ...

15

15

Code Data

Program Counter

Translation to an abstract machine model

Very simple machine model:

  • codememory: −array of machine instructions − contents is static
  • datamemory: −array of data values − contents change during execu- tion
  • programcounter (PC): contains label (index) of the next instruc- tion
  • machinecycle: fetch-incre- ment-decode-execute

Example: Operational semantics

16

16

Code Data

Program Counter

Machine instructions:

• I/O

put mi − get mj

  • Assignments − mi := num − mi := mj − mi := mj op mk
  • Branches − goto li − ifz mi goto lj

Here, mi , mj , mk denote data references; li , lj denote labels

mj :

li :

Machine instructions

L. Dillon CSE 452, Fall 2008

17

ei

e (^) j op e (^) k

code for ej code for ek m (^) i := m (^) j op mk

e (^) i

num

m (^) i := m (^) j

ei

( ej )

code for e (^) j mi := mj

ei

id j

Rules for translating expressions Convention: m (^) j is a unique memory reference for the expression e (^) j / variable id j

m (^) i := num

computes ej & stores the value in m (^) j

L. Dillon CSE 452, Fall 2008

18

ei

ej op e (^) k

code for e (^) j code for ek m (^) i := mj op mk

ei

num

mi := mj

ei

( e (^) j )

code for e (^) j mi := mj

ei

id j

Rules for translating expressions Convention: m (^) j is a unique memory reference for the expression e (^) j / variable id j

m (^) i := num

computes ek & stores the value in m (^) k

19

19

ei

e (^) j op e (^) k

code for ej code for ek m (^) i := m (^) j op mk

e (^) i

num

m (^) i := m (^) j

ei

( ej )

code for e (^) j mi := mj

ei

id j

Rules for translating expressions Convention: m (^) j is a unique memory reference for the expression e (^) j / variable id j

m (^) i := num

computes ei & stores the value in m (^) i

20

20

Example: translating expressions

2 ***** ( a - b )

a b

e

e e

e

e e

L. Dillon CSE 452, Fall 2008

25

Example: translating expressions

a 6 b 7

e 3

e 4 e 5

e 2

e 0

e (^1)

  • Associate a unique memory reference (mi ) with each ex- pression (ej ) and each variable ( id j ) - Apply translation rules (left-to-right, bottom up)

e (^) i

id j

m (^) i := mj

L. Dillon CSE 452, Fall 2008

26

Example: translating expressions

a 6 b 7

e 3

e 4 e 5

e 2

e 0

e 1

  • Associate a unique memory reference (mi ) with each ex- pression (ej ) and each variable ( id j ) - Apply translation rules (left-to-right, bottom up)

ei

id j

m (^) i := mj

27

27

Example: translating expressions

a 6 b 7

e 3

e 4 e 5

e 2

e 0

e (^1)

  • Associate a unique memory reference (mi ) with each ex- pression (ej ) and each variable ( id j ) - Apply translation rules (left-to-right, bottom up)

e (^) i

id j

m (^) i := mj

28

28

Example: translating expressions

a 6 b 7

e 3

e 4 e 5

e 2

e 0

e 1

  • Associate a unique memory reference (mi ) with each ex- pression (ej ) and each variable ( id j ) - Apply translation rules (left-to-right, bottom up)

e (^) i

ej op ek

code for e (^) j code for e (^) k mi := mj op mk

L. Dillon CSE 452, Fall 2008

29

Example: translating expressions

a 6 b 7

e 3

e 4 e 5

e 2

e 0

e (^1)

  • Associate a unique memory reference (mi ) with each ex- pression (ej ) and each variable ( id j ) - Apply translation rules (left-to-right, bottom up)

e (^) i

ej op ek

code for ej code for e (^) k mi := mj op mk

L. Dillon CSE 452, Fall 2008

30

Example: translating expressions

a 6 b 7

e 3

e 4 e 5

e 2

e 0

e 1

  • Associate a unique memory reference (mi ) with each ex- pression (ej ) and each variable ( id j ) - Apply translation rules (left-to-right, bottom up)

31

31

Ambiguity

e 2

e 0

e 1 *****

e 3 - e 4

a b

e 2

e 0

e 1 -

e 3 ***** e (^4)

2 a

b

2 ***** a - b

Suppose a = 5 and b = 1

  • what will the code from the left tree calculate for “ 2 ***** a - b”?
  • what will the code from the right tree calculate for “ 2 ***** a - b”? How might a compiler resolve such ambiguities?

32

32

Rules for translating statements Conventions:

  • lj is a label marking the code for statement Sj
  • lbeg & lend are labels marking the start & end of program P
  • mj is a reference for expression ej / variable id j

&^ l^ i :^ get^ m^ j

Si

read id j

code for ej put mj

&^ li :

Si

write ej

code for ek mj := m (^) k

l (^) i :

S (^) i

vj := ek

code for Si exit

lbeg:

P

S (^) i

code for Sj code for Sk

li :

Si

Sj ; Sk

&^ li^ :code for^ Sj

S (^) i

{ S (^) j

prog end

lend:

L. Dillon CSE 452, Fall 2008

37

Translating statements

S 2 ; S 3

P

prog S 1 end

read x 4 if e 5 then S 6 else S 7

e 8 > e 9

x 4 0 write

e 10

write

e 11

  • Associate − a unique label, l (^) j , with each statement, S (^) j − a uniquememory reference, mj , with each expression ej / (distinct) variable, id j
  • Determinethe label, nj , of the statement “after” each statement, S (^) j − proceed top-down − based on the production that yields Sj P

prog S^ i end

use: ni = lend

L. Dillon CSE 452, Fall 2008

38

Translating statements

S 2 ; S 3

P

prog S 1 end

read x 4 if e 5 then S 6 else S 7

e 8 > e 9

x 4 0 write

e 10

write

e 11

  • Associate − a unique label, l (^) j , with each statement, Sj − a uniquememory reference, m (^) j , with each expression ej / (distinct) variable, id j
  • Determinethe label, n (^) j , of the statement “after” each statement, Sj − proceed top-down − based on the production that yields S (^) j P

prog Si end

use: ni = lend

39

39

Translating statements

S 2 ; S 3

P

prog S 1 end

read x 4

if e 5 then S 6 else S 7

e 8 > e 9

x 4 0 write

e 10

write

e 11

  • Associate − a unique label, l (^) j , with each statement, S (^) j − a uniquememory reference, mj , with each expression ej / (distinct) variable, id j
  • Determinethe label, nj , of the statement “after” each statement, S (^) j − proceed top-down − based on the production that yields Sj

40

40

Translating statements

S 2 ; S 3

P

prog S 1 end

read x 4

if e 5 then S 6 else S 7

e 8 > e 9

x 4 0 write

e 10

write

e 11

  • Associate − a unique label, l (^) j , with each statement, Sj − a uniquememory reference, m (^) j , with each expression ej / (distinct) variable, id j
  • Determinethe label, n (^) j , of the statement “after” each statement, Sj − proceed top-down − based on the production that yields S (^) j

use: nj = lk n (^) k = ni

Si

S (^) j ; Sk

L. Dillon CSE 452, Fall 2008

41

Translating statements

S 2 ; S 3

P

prog S 1 end

read x 4 if e 5 then S 6 else S 7

e 8 > e 9

x 4 0 write

e 10

write

e 11

  • Associate − a unique label, l (^) j , with each statement, S (^) j − a uniquememory reference, mj , with each expression ej / (distinct) variable, id j
  • Determinethe label, nj , of the statement “after” each statement, S (^) j − proceed top-down − based on the production that yields Sj

L. Dillon CSE 452, Fall 2008

42

Translating statements

S 2 ; S 3

P

prog S 1 end

read x 4 if e 5 then S 6 else S 7

e 8 > e 9

x 4 0 write

e 10

write

e 11

  • Associate − a unique label, l (^) j , with each statement, Sj − a uniquememory reference, m (^) j , with each expression ej / (distinct) variable, id j
  • Determinethe label, n (^) j , of the statement “after” each statement, Sj − proceed top-down − based on the production that yields S (^) j

use: nk = ni nq = ni

S (^) i

if ej then^ S^ k else S^ q

43

43

Translating statements

S 2 ; S 3

P

prog S 1 end

read x 4

if e 5 then S 6 else S 7

e 8 > e 9

x 4 0 write

e 10

write

e 11

  • Associate − a unique label, l (^) j , with each statement, S (^) j − a uniquememory reference, mj , with each expression ej / (distinct) variable, id j
  • Determinethe label, nj , of the statement “after” each statement, S (^) j − proceed top-down − based on the production that yields Sj
  • Apply the translation rules (top-down)

44

44

Translating statements

S 2 ; S 3

P

prog S 1 end

read x 4

if e 5 then S 6 else S 7

e 8 > e 9

x 4 0 write

e 10

write

e 11

L. Dillon CSE 452, Fall 2008

49

Translating statements

S 2 ; S 3

P

prog S 1 end

read x 4 if e 5 then S 6 else S 7

e 8 > e 9

x 4 0 write

e 10

write

e 11

L. Dillon CSE 452, Fall 2008

50

Foundations: implementation

Compiled languages

Compiler

Real Machine

(CPU, memory, ...)

Source Program

... if (x == 0) { x = x + 1; } ...

Target Program

... cmp (1000), $ bne L add (1000), $ L: ...

Data

Output

Source and target programs have the same semantics.

Target program is in machine or assembly code.

Machine interpreter executes the target program.

Compiler translates the source program into a target program

51

51

Foundations: implementation

Typical compiler:

Source Program

Syntax analyzer (parser)

Lexical analyzer (scanner)

Semantic analyzer (type checker) Intermediate code generator

Code optimizer

Code generator

Target Program

52

52

Foundations: implementation

Interpreted languages

Source Program

Interpreter

Data

Output

Software interpreter “executes” source program instructions, often interactively.

Traditionally: Read - Eval - Print loop

53

Foundations: implementation

Typical interpreter:

Source Program

Data

Output

Compiler

Target Program

Source Language-specific Virtual Machine

54

Java: implementation

Java Compiler

Java Virtual Machine (JVM)

Source Program

... if (x == 0) { x = x + 1; } ...

Target Program

... iload_ iconst_ ifne L inc store_ L:...

Data

Output

Compromise between compilation/interpretation

Important design goals:

_- Portability

  • Remote execution
  • Safety
  • Reliability
  • Multithreaded execution
  • Simplicity
  • Familiarity
  • Efficiency_