Lecture 4: Syntax Specification

22
COMP 144 Programming Language Concepts Felix Hernandez-Campos 1 Lecture 4: Lecture 4: Syntax Specification Syntax Specification COMP 144 Programming Language COMP 144 Programming Language Concepts Concepts Spring 2002 Spring 2002 Felix Hernandez-Campos Felix Hernandez-Campos Jan 16 Jan 16 The University of North Carolina at The University of North Carolina at Chapel Hill Chapel Hill

description

The University of North Carolina at Chapel Hill. Lecture 4: Syntax Specification. COMP 144 Programming Language Concepts Spring 2002. Felix Hernandez-Campos Jan 16. Phases of Compilation. Syntax Analysis. Syntax: - PowerPoint PPT Presentation

Transcript of Lecture 4: Syntax Specification

Page 1: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

11

Lecture 4: Lecture 4: Syntax SpecificationSyntax Specification

COMP 144 Programming Language ConceptsCOMP 144 Programming Language Concepts

Spring 2002Spring 2002

Felix Hernandez-CamposFelix Hernandez-Campos

Jan 16Jan 16

The University of North Carolina at Chapel HillThe University of North Carolina at Chapel Hill

Page 2: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

22

Phases of CompilationPhases of Compilation

Page 3: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

33

Syntax AnalysisSyntax Analysis

• Syntax:Syntax:– Webster’s definition: Webster’s definition: 1 a : the way in which linguistic 1 a : the way in which linguistic

elements (as words) are put together to form constituents elements (as words) are put together to form constituents (as phrases or clauses)(as phrases or clauses)

• The syntax of a programming languageThe syntax of a programming language– Describes its formDescribes its form

» i.e.i.e. Organization of tokens Organization of tokens (elements)(elements)

– Formal notationFormal notation» Context Free Grammars (CFGs)Context Free Grammars (CFGs)

Page 4: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

44

Review: Formal definition of tokensReview: Formal definition of tokens

• A set of tokens is a set of strings over an alphabetA set of tokens is a set of strings over an alphabet– {read, write, +, -, *, /, :=, 1, 2, …, 10, …, 3.45e-3, …}{read, write, +, -, *, /, :=, 1, 2, …, 10, …, 3.45e-3, …}

• A set of tokens is a A set of tokens is a regular setregular set that can be defined by that can be defined by comprehension using a comprehension using a regular expressionregular expression

• For every regular set, there is a For every regular set, there is a deterministic finite deterministic finite automatonautomaton (DFA) that can recognize it (DFA) that can recognize it

– i.e.i.e. determine whether a string belongs to the set or not determine whether a string belongs to the set or not– Scanners extract tokens from source code in the same way Scanners extract tokens from source code in the same way

DFAs determine membershipDFAs determine membership

Page 5: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

55

Review: Regular ExpressionsReview: Regular Expressions

• A regular expression (RE) is:A regular expression (RE) is:– A single characterA single character– The empty string, The empty string, – The The concatenationconcatenation of two regular expressions of two regular expressions

» Notation:Notation: RE RE11 RE RE22 ( (i.e. i.e. RERE11 followed by RE followed by RE22))

– The The unionunion of two regular expressionsof two regular expressions» Notation: Notation: RERE11 | RE | RE22

– The The closureclosure of a regular expression of a regular expression» Notation: Notation: RE*RE*» * is known as the * is known as the Kleene starKleene star» * * represents the concatenation of 0 or more stringsrepresents the concatenation of 0 or more strings

Page 6: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

66

Review: Token Definition ExampleReview: Token Definition Example

• Numeric literals in PascalNumeric literals in Pascal– Definition of the token Definition of the token unsigned_numberunsigned_number

digit digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

unsigned_integer unsigned_integer digitdigit digitdigit**

unsigned_number unsigned_number unsigned_integer unsigned_integer ( ( . ( ( . unsigned_integer unsigned_integer ) | ) | ) )( ( e ( + | – | ( ( e ( + | – | ) ) unsigned_integer unsigned_integer )) | | ) )

• Recursion is not allowed!Recursion is not allowed!

Page 7: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

77

ExerciseExercise

digit digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

unsigned_integer unsigned_integer digitdigit digitdigit**

unsigned_number unsigned_number unsigned_integer unsigned_integer ( ( ( ( .. unsigned_integer unsigned_integer ) | ) | ) )( ( e ( + | – | ( ( e ( + | – | ) ) unsigned_integer unsigned_integer )) | | ) )

• Regular expression forRegular expression for– Decimal numbersDecimal numbers

number number … …

Page 8: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

88

ExerciseExercise

digit digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

unsigned_integer unsigned_integer digitdigit digitdigit**

unsigned_number unsigned_number unsigned_integer unsigned_integer ( ( ( ( .. unsigned_integer unsigned_integer ) | ) | ) )( ( e ( + | – | ( ( e ( + | – | ) ) unsigned_integer unsigned_integer )) | | ) )

• Regular expression forRegular expression for– Decimal numbersDecimal numbers

number number ( + | – | ( + | – | ) ) unsigned_integer unsigned_integer ( ( ( ( unsigned_integer unsigned_integer ) | ) | ) )

Page 9: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

99

ExerciseExercise

digit digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

unsigned_integer unsigned_integer digitdigit digitdigit**

unsigned_number unsigned_number unsigned_integer unsigned_integer ( ( ( ( .. unsigned_integer unsigned_integer ) | ) | ) )( ( e ( + | – | ( ( e ( + | – | ) ) unsigned_integer unsigned_integer )) | | ) )

• Regular expression forRegular expression for– IdentifiersIdentifiers

identifier identifier ……

Page 10: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1010

ExerciseExercise

digit digit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

unsigned_integer unsigned_integer digitdigit digitdigit**

unsigned_number unsigned_number unsigned_integer unsigned_integer ( ( ( ( .. unsigned_integer unsigned_integer ) | ) | ) )( ( e ( + | – | ( ( e ( + | – | ) ) unsigned_integer unsigned_integer )) | | ) )

• Regular expression forRegular expression for– IdentifiersIdentifiers

identifier identifier letter letter ( ( letterletter | digit | | digit | )* )*

letter letter a | b | c | … | z a | b | c | … | z

Page 11: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1111

Context Free GrammarsContext Free Grammars

• CFGsCFGs– Add recursion to regular expressionsAdd recursion to regular expressions

» Nested constructionsNested constructions

– NotationNotationexpressionexpression identifieridentifier | | numbernumber | | -- expressionexpression | | (( expressionexpression )) | | expressionexpression operatoroperator expressionexpressionoperator operator ++ | | -- | | ** | | //

» Terminal symbolsTerminal symbols» Non-terminal symbolsNon-terminal symbols» Production rule (i.e. substitution rule)Production rule (i.e. substitution rule)

terminal symbol terminal symbol terminal and non-terminal symbols terminal and non-terminal symbols

Page 12: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1212

Backus-Naur FormBackus-Naur Form

• Backus-Naur Form (BNF)Backus-Naur Form (BNF)– Equivalent to CFGs in powerEquivalent to CFGs in power– CFGCFG

expressionexpression identifieridentifier | | numbernumber | | -- expressionexpression | | (( expressionexpression )) | | expressionexpression operatoroperator expressionexpressionoperator operator ++ | | - - | | * * | | //

– BNFBNFexpressionexpression identifieridentifier | | numbernumber | - | - expressionexpression | ( | ( expressionexpression ) ) | | expressionexpression operatoroperator expressionexpressionoperatoroperator ++ | | - - | | * * | | //

Page 13: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1313

Extended Backus-Naur FormExtended Backus-Naur Form

• Extended Backus-Naur Form (EBNF)Extended Backus-Naur Form (EBNF)– Adds some convenient symbolsAdds some convenient symbols

» UnionUnion ||» Kleene starKleene star **» Meta-level parenthesesMeta-level parentheses ( )( )

– It has the same expressive powerIt has the same expressive power

Page 14: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1414

Extended Backus-Naur FormExtended Backus-Naur Form

• Extended Backus-Naur Form (EBNF)Extended Backus-Naur Form (EBNF)– It has the same expressive powerIt has the same expressive power

BNFBNFdigitdigit 0 0digitdigit 1 1……digitdigit 9 9unsigned_integerunsigned_integer digitdigitunsigned_integerunsigned_integer digitdigit unsigned_integerunsigned_integer

EBNFEBNFdigitdigit 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9unsigned_integerunsigned_integer digitdigit digitdigit**

Page 15: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1515

DerivationsDerivations

• A derivation shows how to generate a syntactically A derivation shows how to generate a syntactically valid stringvalid string

– Given a CFGGiven a CFG– Example:Example:

» CFGCFG

expressionexpression identifieridentifier | | numbernumber | | -- expressionexpression | | (( expressionexpression )) | | expressionexpression operatoroperator expressionexpressionoperator operator ++ | | - - | | * * | | //

» Derivation ofDerivation of

slope * x + interceptslope * x + intercept

Page 16: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1616

Derivation ExampleDerivation Example

• Derivation of Derivation of slope * x + interceptslope * x + intercept

expressionexpression expressionexpression operatoroperator expressionexpression expressionexpression operatoroperator interceptintercept expressionexpression ++ interceptintercept expressionexpression operatoroperator expressionexpression ++

interceptintercept expressionexpression operatoroperator x x ++ interceptintercept expressionexpression ** x x ++ interceptintercept slopeslope ** x x ++ interceptintercept

expressionexpression * * slopeslope ** x x ++ interceptintercept

» Identifiers were not derived for simplicityIdentifiers were not derived for simplicity

Page 17: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1717

Parse TreesParse Trees

• A parse is graphical representation of a derivationA parse is graphical representation of a derivation

• ExampleExample

Page 18: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1818

Ambiguous GrammarsAmbiguous Grammars

• Alternative parse tree Alternative parse tree – same expressionsame expression– same grammarsame grammar

• This grammar is ambiguousThis grammar is ambiguous

Page 19: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

1919

Designing unambiguous grammarsDesigning unambiguous grammars

• Specify more grammatical structureSpecify more grammatical structure– In our example, left associativity and operator precedenceIn our example, left associativity and operator precedence

» 10 – 4 – 310 – 4 – 3 means (10 – 4) – 3 means (10 – 4) – 3» 3 + 4 * 53 + 4 * 5 means 3 + (4 * 5) means 3 + (4 * 5)

Page 20: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

2020

ExampleExample

• Parse tree for Parse tree for 3 + 4 * 53 + 4 * 5

• Exercise: parse tree for Exercise: parse tree for - 10 / 5 * 8 – 4 - 5- 10 / 5 * 8 – 4 - 5

Page 21: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

2121

Java Language SpecificationJava Language Specification

• Available on-lineAvailable on-line– http://java.sun.com/docs/books/jls/second_edition/html/j.tihttp://java.sun.com/docs/books/jls/second_edition/html/j.ti

tle.doc.htmltle.doc.html

• ExamplesExamples– Comments: Comments:

http://java.sun.com/docs/books/jls/second_edition/html/lexhttp://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48125ical.doc.html#48125

– Multiplicative Operators:Multiplicative Operators: http://java.sun.com/docs/books/jls/second_edition/html/exhttp://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#239829pressions.doc.html#239829

– Unary Operators:Unary Operators: http://java.sun.com/docs/books/jls/second_edition/html/exhttp://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#4990pressions.doc.html#4990

Page 22: Lecture 4:  Syntax Specification

COMP 144 Programming Language ConceptsFelix Hernandez-Campos

2222

Reading AssignmentReading Assignment

• Scott’s Chapter 2Scott’s Chapter 2– Section 2.1.2Section 2.1.2– Section 2.1.3Section 2.1.3

• Java language specificationJava language specification– Chapter 2 (Grammars)Chapter 2 (Grammars)– Glance at chapter 3Glance at chapter 3– Glance at sections 15.17, 15.18 and 15.15Glance at sections 15.17, 15.18 and 15.15