CSC 1800 Organization of Programming Languages Syntax 1 - - PDF document

csc 1800 organization of programming languages
SMART_READER_LITE
LIVE PREVIEW

CSC 1800 Organization of Programming Languages Syntax 1 - - PDF document

CSC 1800 Organization of Programming Languages Syntax 1 Questions What is a computer program? What is a programming language? Describe the alphabet available to you to create programs 2 2 1 Programs, formally Let A be the


slide-1
SLIDE 1

1

CSC 1800 Organization of Programming Languages

Syntax

2

Questions

⚫ What is a computer program? ⚫ What is a programming language? ⚫ Describe the alphabet available to you to create

programs

1 2

slide-2
SLIDE 2

2

3

Programs, formally

⚫ Let A be the alphabet of a programming language L,

hence a subset of all the keyboard-producible characters.

⚫ Let A* be the set of finite sequences of characters of A. ⚫ L is a subset of A*. That is, any program P in

L is a sequence of characters in A.

⚫ Why is this definition not completely correct?

4

Syntax

⚫ The description of a language specifying structurally

correct phrases in the language. As opposed to

Semantics

The meaning of phrases in the language

3 4

slide-3
SLIDE 3

3

5

Syntax (2)

and

Pragmatics

The practical use of the language

Communicating algorithms to humans

Laying out a program in readable form

Purpose of the language

User interface to the language including IDE

Efficiency of generated code

6

Simple Syntax

<letter> ::= a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p| q|r|s|t|u|v|w|x|y|z Non-terminal: <letter> Terminals: lower case letters (26 choices) | means “or”

5 6

slide-4
SLIDE 4

4

7

Simple Syntax (2)

<letter seq> ::= <letter> | <letter seq><letter> Notes:

⚫ Right recursion (or is it left recursion?) ⚫ Does the recursive order make a difference? ⚫ What are permissible lengths of letter sequences?

8

Simple Syntax (3)

<digit> ::= 0|1|2|3|4|5|6|7|8|9 <digit string> ::= <digit> | <digit string><digit> Your turn:

⚫ Create the syntax specification for an integer

7 8

slide-5
SLIDE 5

5

9

BNF

⚫ Backus-Naur Form (or Backus Normal Form)

John Backus, developer of Fortran

Peter Naur

Revised Report on the Algorithmic Language Algol 60, Computer Journal, 5(1963), 349-367. ⚫ Use for

Checking a symbol string for syntactic correctness

Generating a correct symbol string ⚫ The examples on the previous slide use BNF

10

BNF

::= is defined as |

  • r

< > syntactic category, grammatical category

9 10

slide-6
SLIDE 6

6

11

Extended BNF

For those who dislike recursion:

⚫ [ ]

surround an optional part

⚫ { } surround a repeated part, repeated 0 or more times ⚫ ( )

are used to clarify hierarchy

12

Sequences

In many instances, we need to specify a sequence of

  • items. This may be done recursively:

<seq> ::= <id> | <seq> , <id>

  • r

<seq> ::= <id> | <id> , <seq>

  • r (using EBNF)

<seq> ::= <id> { , <id> }

11 12

slide-7
SLIDE 7

7

13

Context Free Grammar

⚫ Defined with four (4) elements:

terminal symbols

⚫ formed as strings of the alphabet of the language ⚫ lexical elements of the language

non-terminal symbols

⚫ each specifies a grammatical category, syntactic category, or set of strings

  • f terminal symbols

special non-terminal, the start symbol

⚫ appears only on LHS of a production

productions

⚫ recursively defines all non-terminal symbols by showing how terminal and

non-terminal symbols may be combined

14

Context Free Language

⚫ Defined by a context free grammar ⚫ The set of all strings of terminal symbols generated by

the context free grammar

13 14

slide-8
SLIDE 8

8

15

Parse Tree

⚫ A labeled rooted tree ⚫ Root labeled with start symbol ⚫ Interior nodes labeled with non-terminal symbols

(syntactic categories)

⚫ Leaves labeled with terminal symbols

16

Parse Tree Example

A = B * ( A + C )

15 16

slide-9
SLIDE 9

9

17

Grammar of Expressions

Using a simplified EBNF S ::= E E ::= T | E + T | E – T T ::= F | T*F | T/F F ::= (E) | I | N

What is this language?

Key: S – statement E – expression T – term F – factor I – identifier N – number

18

Expression Parse Trees

Your turn:

1.

Build a parse tree for a+b*c

2.

Build a parse tree for a-b-c Infer rules for associativity and distributivity from the structure of the grammar. [Note: example trees on next slide… no peeking]

17 18

slide-10
SLIDE 10

10

19

Parse Trees

a+b*c * + a b c + * a b c

20

Ambiguity

Some grammars lead to distinct parse trees for the same expression. Questions:

1.

What does distinct mean?

2.

Is this situation bad?

3.

What can be done?

19 20

slide-11
SLIDE 11

11

21

Ambiguity Revealed

A = B + C * A

21