CSCI 2320 Syntax MOHAMMAD T. IRFAN Review of defini@ons (see - - PDF document

csci 2320 syntax
SMART_READER_LITE
LIVE PREVIEW

CSCI 2320 Syntax MOHAMMAD T. IRFAN Review of defini@ons (see - - PDF document

9/15/17 CSCI 2320 Syntax MOHAMMAD T. IRFAN Review of defini@ons (see classnote) Syntax (form): expressed by grammar CFG/BNF: (1) terminal (2) nonterminal (3) producEon SentenEal form Sentence Language LeG-recursive vs.


slide-1
SLIDE 1

9/15/17 1

CSCI 2320 Syntax

MOHAMMAD T. IRFAN

Review of defini@ons (see classnote)

§ Syntax (form): expressed by grammar § CFG/BNF: (1) terminal (2) nonterminal (3) producEon § SentenEal form § Sentence § Language § LeG-recursive vs. right-recursive producEon § DerivaEon & parse tree

§ LeG-most vs. right-most derivaEon

§ Ambiguous grammar: 2 parse trees for the same sentence

slide-2
SLIDE 2

9/15/17 2

Resolving ambiguity: C++

§ “The else ambiguity is resolved by connecEng an else with the last encountered else-less if.” [Stroustrup, 1991]

C++ Grammar (on Blackboard)

Resolving ambiguity: Java

hXps://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.5 § Statement: IfThenStatement IfThenElseStatement ... § IfThenStatement: if ( Expression ) Statement § IfThenElseStatement: if ( Expression ) StatementNoShortIf else Statement

Anything other than simple if- then (known as short if)

slide-3
SLIDE 3

9/15/17 3

Resolving ambiguity: Algol 68

§ By syntax of if-fi § Example: if (x > 0) if (y > 0) y := -5; else y := 5; fi fi

What about Python?

Extended BNF (EBNF)

ITERATIONS, OPTIONS, AND CHOICES BNF IS AS POWERFUL AS EBNF (SEE CLASSNOTE)

slide-4
SLIDE 4

9/15/17 4

Grammar for CLite

CONCRETE SYNTAX (HIGHER LEVELS OF ABSTRACTION) LEXICAL SYNTAX (LOW LEVEL: TYPED THINGS)

Higher precedence

Precedence of operators in C

hXp://en.cppreference.com/w/c/language/operator_precedence

slide-5
SLIDE 5

9/15/17 5

Concrete Syntax (EBNF): Hierarchy of categories/abstrac@ons

Program → int main ( ) { Declarations Statements } Declarations → { Declaration } Declaration → Type Identifier [ [ Integer ] ] { , Identifier [ [ Integer ] ] } ; Type → int | bool | float | char Statements → { Statement } Statement → ; | Block | Assignment | IfStatement | WhileStatement Block → { Statements } Assignment → Identifier [ [ Expression ] ] = Expression ; IfStatement → if ( Expression ) Statement [ else Statement ] WhileStatement → while ( Expression ) Statement Expression → Conjunction { || Conjunction } Conjunction → Equality { && Equality } Equality → Relation [ EquOp Relation ] EquOp → == | != Relation → Addition [ RelOp Addition ] RelOp → < | <= | > | >= Addition → Term { AddOp Term } AddOp → + | - Term → Factor { MulOp Factor } MulOp → * | / | % Factor → [ UnaryOp ] Primary UnaryOp → - | ! Primary → Identifier [ [ Expression ] ] | Literal | ( Expression ) | Type ( Expression )

Concrete Syntax (cont…)

Note: Operators and punctuaEon symbols are part

  • f "lexical syntax" (next)
slide-6
SLIDE 6

9/15/17 6

Lexical Syntax

Input: a stream of characters from the ASCII set, keyed by a programmer. Output: a stream of tokens or basic symbols, classified as follows:

  • IdenEfiers

e.g., Stack, x, i, push

  • Literals

e.g., 123, 'x', 3.25, true

  • Keywords

bool char else false float if int main true while

  • Operators

= || && == != < <= > >= + - * / !

  • PunctuaEon

; , { } ( )

Identifier → Letter { Letter | Digit } Letter → a | b | … | z | A | B | … | Z Digit → 0 | 1 | … | 9 Literal → Integer | Boolean | Float | Char Integer → Digit { Digit } Boolean → true | False Float → Integer . Integer Char → ‘ ASCII Char ‘

Lexical Syntax: Lexemes à Tokens

slide-7
SLIDE 7

9/15/17 7

Abstract Syntax Tree

Parse Tree for z = x + 2*y;

  • Fig. 2.9
slide-8
SLIDE 8

9/15/17 8 Abstract Syntax Tree for z = x + 2*y;

  • Fig. 2.10

Op@onal Reading

Pages 49 -- 54