Week 2: Syntax Specification, Grammars

Similar documents
Introduction to Lexical Analysis

Non-deterministic Finite Automata (NFA)

CSE 3302 Programming Languages Lecture 2: Syntax

CPS 506 Comparative Programming Languages. Syntax Specification

Formal Languages and Compilers Lecture VI: Lexical Analysis

CS 314 Principles of Programming Languages. Lecture 3

CS 314 Principles of Programming Languages

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Syntax. A. Bellaachia Page: 1

Part 5 Program Analysis Principles and Techniques

Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

Lexical Analysis. Introduction

Introduction to Lexical Analysis

CS 403 Compiler Construction Lecture 3 Lexical Analysis [Based on Chapter 1, 2, 3 of Aho2]

Compiler Construction D7011E

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

Languages and Compilers

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

Lexical Analysis. Chapter 2

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

Lexical Analysis. Lecture 2-4

Lexical Analysis. Lecture 3. January 10, 2018

Structure of Programming Languages Lecture 3

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Building Compilers with Phoenix

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Syntax. In Text: Chapter 3

CSE 413 Programming Languages & Implementation. Hal Perkins Autumn 2012 Grammars, Scanners & Regular Expressions

CSCI312 Principles of Programming Languages!

CSCE 314 Programming Languages

Chapter 3. Describing Syntax and Semantics ISBN

Optimizing Finite Automata

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

Lexical Analyzer Scanner

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Introduction to Lexing and Parsing

Lexical Analysis. Lecture 3-4

CSE 413 Programming Languages & Implementation. Hal Perkins Winter 2019 Grammars, Scanners & Regular Expressions

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CS415 Compilers. Lexical Analysis

CMSC 330: Organization of Programming Languages

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

Lecture 4: Syntax Specification

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

Concepts Introduced in Chapter 3. Lexical Analysis. Lexical Analysis Terms. Attributes for Tokens

The Front End. The purpose of the front end is to deal with the input language. Perform a membership test: code source language?

Scanners. Xiaokang Qiu Purdue University. August 24, ECE 468 Adapted from Kulkarni 2012

Lexical Analyzer Scanner

Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

CS308 Compiler Principles Lexical Analyzer Li Jiang

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Implementation of Lexical Analysis

COP 3402 Systems Software Syntax Analysis (Parser)

Principles of Programming Languages COMP251: Syntax and Grammars

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Interpreter. Scanner. Parser. Tree Walker. read. request token. send token. send AST I/O. Console

Compiler Construction

Week 3: Compilers and Interpreters

Chapter 4. Lexical analysis. Concepts. Lexical scanning Regular expressions DFAs and FSAs Lex. Lexical analysis in perspective

Implementation of Lexical Analysis

2. Lexical Analysis! Prof. O. Nierstrasz!

A Simple Syntax-Directed Translator

Syntax Intro and Overview. Syntax

Monday, August 26, 13. Scanners

Context-Free Grammars

Programming Language Syntax and Analysis

Syntax and Grammars 1 / 21

Wednesday, September 3, 14. Scanners

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

Announcements! P1 part 1 due next Tuesday P1 part 2 due next Friday

Wednesday, August 31, Parsers

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Dr. D.M. Akbar Hussain

ICOM 4036 Spring 2004

Administrivia. Lexical Analysis. Lecture 2-4. Outline. The Structure of a Compiler. Informal sketch of lexical analysis. Issues in lexical analysis

CMSC 330: Organization of Programming Languages. Context Free Grammars

CMSC 330: Organization of Programming Languages. Context Free Grammars

Describing Syntax and Semantics

UNIT -2 LEXICAL ANALYSIS

Lexical Analysis. Finite Automata

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

ECE251 Midterm practice questions, Fall 2010

Lexical Analysis. Finite Automata

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

CS 314 Principles of Programming Languages

Compiler Construction

CMSC 330: Organization of Programming Languages. Context Free Grammars

Formal Languages. Formal Languages

Transcription:

CS320 Principles of Programming Languages Week 2: Syntax Specification, Grammars Jingke Li Portland State University Fall 2017 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 1/ 62

Words and Sentences Recall Language = { Sentences } Sentence = Sequence of words Grammar = Rules for constructing sentences PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 2/ 62

Words and Sentences Recall Language = { Sentences } Sentence = Sequence of words Grammar = Rules for constructing sentences Questions: 1. What information about words does a grammar need? PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 2/ 62

Words and Sentences Recall Language = { Sentences } Sentence = Sequence of words Grammar = Rules for constructing sentences Questions: 1. What information about words does a grammar need? 2. Do we need rules for specifying words? PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 2/ 62

Words and Sentences Let s look at an example. Grammar: Sentence = NounPhrase VerbPhrase NounPhrase = Noun Article Noun VerbPhrase = Verb Verb PrepPrase PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 3/ 62

Words and Sentences Let s look at an example. Grammar: Sentences: Sentence = NounPhrase VerbPhrase NounPhrase = Noun Article Noun VerbPhrase = Verb Verb PrepPrase (1) Dogs run. Birds fly. Fish swim. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 3/ 62

Words and Sentences Let s look at an example. Grammar: Sentence = NounPhrase VerbPhrase NounPhrase = Noun Article Noun VerbPhrase = Verb Verb PrepPrase Sentences: (1) Dogs run. Birds fly. Fish swim. (2) Fish run. Dogs fly. Birds swim. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 3/ 62

Words and Sentences Let s look at an example. Grammar: Sentence = NounPhrase VerbPhrase NounPhrase = Noun Article Noun VerbPhrase = Verb Verb PrepPrase Sentences: (1) Dogs run. Birds fly. Fish swim. (2) Fish run. Dogs fly. Birds swim. Observation: Both group of sentences are syntactically valid. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 3/ 62

Words and Sentences Let s look at an example. Grammar: Sentence = NounPhrase VerbPhrase NounPhrase = Noun Article Noun VerbPhrase = Verb Verb PrepPrase Sentences: (1) Dogs run. Birds fly. Fish swim. (2) Fish run. Dogs fly. Birds swim. Observation: Both group of sentences are syntactically valid. Grammars do not deal with individual words, they deal with categories of words. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 3/ 62

Words and Sentences For English, Categories of words = Parts of Speech, i.e. Nouns, Pronouns, Verbs, Adjectives, Adverbs, Conjunctions, Prepositions, and Interjections PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 4/ 62

Words and Sentences For English, Categories of words = Parts of Speech, i.e. Nouns, Pronouns, Verbs, Adjectives, Adverbs, Conjunctions, Prepositions, and Interjections For PLs, Categories of words = Token Types, e.g. Keywords, Identifiers, Literals, Operators, etc. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 4/ 62

Tokens A program is built from tokens. /* A toy C program */ int main(void) { int a, b, s; printf("enter two integers: "); scanf("%d %d", &a, &b); s = a + 2 * b; printf("%d + 2*%d = %d\n", a, b, s); } PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 5/ 62

Tokens A program is built from tokens. /* A toy C program */ int main(void) { int a, b, s; printf("enter two integers: "); scanf("%d %d", &a, &b); s = a + 2 * b; printf("%d + 2*%d = %d\n", a, b, s); } Token = Smallest program entity that is meaningful to the language PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 6/ 62

Common Token Types Keywords int, void, printf, scanf,... A finite set of language-defined names PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 7/ 62

Common Token Types Keywords int, void, printf, scanf,... A finite set of language-defined names Each keyword is typically given a token type of its own, since they are not interchangeable in a grammar PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 7/ 62

Common Token Types Keywords int, void, printf, scanf,... A finite set of language-defined names Each keyword is typically given a token type of its own, since they are not interchangeable in a grammar Identifiers main, a, b, s,... A potentially infinite set of user-defined names PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 7/ 62

Common Token Types Keywords int, void, printf, scanf,... A finite set of language-defined names Each keyword is typically given a token type of its own, since they are not interchangeable in a grammar Identifiers main, a, b, s,... A potentially infinite set of user-defined names All IDs form a single token type PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 7/ 62

Common Token Types Keywords int, void, printf, scanf,... A finite set of language-defined names Each keyword is typically given a token type of its own, since they are not interchangeable in a grammar Identifiers main, a, b, s,... A potentially infinite set of user-defined names All IDs form a single token type Operators and Delimiters (, ), {, },,, ;, =, +, *,... PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 7/ 62

Common Token Types Keywords int, void, printf, scanf,... A finite set of language-defined names Each keyword is typically given a token type of its own, since they are not interchangeable in a grammar Identifiers main, a, b, s,... A potentially infinite set of user-defined names All IDs form a single token type Operators and Delimiters (, ), {, },,, ;, =, +, *,... Each operator/delimiter is typically a separate token type PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 7/ 62

Common Token Types Keywords int, void, printf, scanf,... A finite set of language-defined names Each keyword is typically given a token type of its own, since they are not interchangeable in a grammar Identifiers main, a, b, s,... A potentially infinite set of user-defined names All IDs form a single token type Operators and Delimiters (, ), {, },,, ;, =, +, *,... Each operator/delimiter is typically a separate token type Literals Strings Enter two integers:, %d %d,... Integers 2, 365,... Doubles 0.5, 3.14,...... PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 7/ 62

Common Token Types Keywords int, void, printf, scanf,... A finite set of language-defined names Each keyword is typically given a token type of its own, since they are not interchangeable in a grammar Identifiers main, a, b, s,... A potentially infinite set of user-defined names All IDs form a single token type Operators and Delimiters (, ), {, },,, ;, =, +, *,... Each operator/delimiter is typically a separate token type Literals Strings Enter two integers:, %d %d,... Integers 2, 365,... Doubles 0.5, 3.14,...... Each literal group form a single token type PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 7/ 62

Lexemes Lexeme = A particular, meaningful sequence of characters from a program PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 8/ 62

Lexemes Lexeme = A particular, meaningful sequence of characters from a program Each token is associated with a lexeme. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 8/ 62

Lexemes Lexeme = A particular, meaningful sequence of characters from a program Each token is associated with a lexeme. A token type may correspond to a single lexeme Such as the cases of individual keywords, operators, and delimiters In these cases, the token type is often named the same as the lexeme PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 8/ 62

Lexemes Lexeme = A particular, meaningful sequence of characters from a program Each token is associated with a lexeme. A token type may correspond to a single lexeme Such as the cases of individual keywords, operators, and delimiters In these cases, the token type is often named the same as the lexeme Or it may correspond to a collection of lexemes Such as the cases of IDs and literals In these cases, the collection of lexemes typically share a common characteristics, e.g. integer literals are all sequence of digits PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 8/ 62

Non-Token Lexemes Whitespace typically have no significance in the language other than to separate tokens: the space, tab, newline character,... PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 9/ 62

Non-Token Lexemes Whitespace typically have no significance in the language other than to separate tokens: the space, tab, newline character,... Comments have no effect on program s semantics. various forms depending on language PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 9/ 62

Non-Token Lexemes Whitespace typically have no significance in the language other than to separate tokens: the space, tab, newline character,... Comments have no effect on program s semantics. various forms depending on language Illegal Characters characters that do no belong to the language s alphabet. # ^ $ ~ @ PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 9/ 62

Non-Token Lexemes Whitespace typically have no significance in the language other than to separate tokens: the space, tab, newline character,... Comments have no effect on program s semantics. various forms depending on language Illegal Characters characters that do no belong to the language s alphabet. # ^ $ ~ @ Ill-Formed Tokens Not all legal characters form valid tokens. 0789, 0xAG, "abc<eof> PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 9/ 62

Non-Token Lexemes Whitespace typically have no significance in the language other than to separate tokens: the space, tab, newline character,... Comments have no effect on program s semantics. various forms depending on language Illegal Characters characters that do no belong to the language s alphabet. # ^ $ ~ @ Ill-Formed Tokens Not all legal characters form valid tokens. 0789, 0xAG, "abc<eof> Whitespace and comments are filtered out during lexical analysis; while illegal chars and ill-formed tokens are lexical errors, which should caught and reported. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 9/ 62

Common Comment Types Single line: // C, C++, Java -- Haskell ; Lisp, Scheme C Fortran # csh, bash, make PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 10 / 62

Common Comment Types Single line: // C, C++, Java -- Haskell ; Lisp, Scheme C Fortran # csh, bash, make Non-nesting block: /* C, C++, Java */ PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 10 / 62

Common Comment Types Single line: // C, C++, Java -- Haskell ; Lisp, Scheme C Fortran # csh, bash, make Non-nesting block: /* C, C++, Java */ Nesting block: (* Pascal, ML (* allow nesting *) *) {- Haskell {- as well -} -} PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 10 / 62

Token Representation A token is typically represented as an object with attributes: PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 11 / 62

Token Representation A token is typically represented as an object with attributes: Type the key info used by a grammar PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 11 / 62

Token Representation A token is typically represented as an object with attributes: Type the key info used by a grammar Lexeme to distinguish individual member of a group PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 11 / 62

Token Representation A token is typically represented as an object with attributes: Type the key info used by a grammar Lexeme to distinguish individual member of a group Value for numerical tokens It is common for a compiler to convert a numerical token s lexeme to a value, and store it in the token object (in addition to or instead ofthe lexeme). PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 11 / 62

Token Representation A token is typically represented as an object with attributes: Type the key info used by a grammar Lexeme to distinguish individual member of a group Value for numerical tokens It is common for a compiler to convert a numerical token s lexeme to a value, and store it in the token object (in addition to or instead ofthe lexeme). Position line and column numbers (for diagnostic use) PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 11 / 62

Rules for Specifying Words? PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 12 / 62

Rules for Specifying Words? For English, the vocabulary ( 172,000 words) is fixed There is no need to create new words Given a word, it is not hard to find the part of speech(s) it belongs to Just look up a dictionary PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 12 / 62

Rules for Specifying Words? For English, the vocabulary ( 172,000 words) is fixed There is no need to create new words Given a word, it is not hard to find the part of speech(s) it belongs to Just look up a dictionary For PLs, the vocabulary is not fixed PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 12 / 62

Rules for Specifying Words? For English, the vocabulary ( 172,000 words) is fixed There is no need to create new words Given a word, it is not hard to find the part of speech(s) it belongs to Just look up a dictionary For PLs, the vocabulary is not fixed Programmer needs to know how to create new tokens PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 12 / 62

Rules for Specifying Words? For English, the vocabulary ( 172,000 words) is fixed There is no need to create new words Given a word, it is not hard to find the part of speech(s) it belongs to Just look up a dictionary For PLs, the vocabulary is not fixed Programmer needs to know how to create new tokens Compiler needs to know how to recognize tokens (and non-tokens) without a dictionary PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 12 / 62

Rules for Specifying Words? For English, the vocabulary ( 172,000 words) is fixed There is no need to create new words Given a word, it is not hard to find the part of speech(s) it belongs to Just look up a dictionary For PLs, the vocabulary is not fixed Programmer needs to know how to create new tokens Compiler needs to know how to recognize tokens (and non-tokens) without a dictionary the information must come from the lexemes themselves PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 12 / 62

Rules for Specifying Words? For English, the vocabulary ( 172,000 words) is fixed There is no need to create new words Given a word, it is not hard to find the part of speech(s) it belongs to Just look up a dictionary For PLs, the vocabulary is not fixed Programmer needs to know how to create new tokens Compiler needs to know how to recognize tokens (and non-tokens) without a dictionary the information must come from the lexemes themselves We need a mechanism to precisely define tokens. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 12 / 62

Patterns Pattern = A description of lexical features for a set of character sequences PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 13 / 62

Patterns Pattern = A description of lexical features for a set of character sequences Examples: Sequence of digits 1234, 503, 00001 Sequence within quotes "1234", "abc", "@+-*" Sequence starts with letter a a1234, abc, a@+-* PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 13 / 62

Patterns Pattern = A description of lexical features for a set of character sequences Examples: Sequence of digits 1234, 503, 00001 Sequence within quotes "1234", "abc", "@+-*" Sequence starts with letter a a1234, abc, a@+-* For a PL, lexemes belonging to the same token type typically share a common pattern. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 13 / 62

Patterns Pattern = A description of lexical features for a set of character sequences Examples: Sequence of digits 1234, 503, 00001 Sequence within quotes "1234", "abc", "@+-*" Sequence starts with letter a a1234, abc, a@+-* For a PL, lexemes belonging to the same token type typically share a common pattern. The preferred tool for precisely specifying patterns is regular expression. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 13 / 62

Review: Regular Expressions RE defines strings over a finite alphabet with three operations: alternate concatenate (omitable) repeat0ormoretimes PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 14 / 62

Review: Regular Expressions RE defines strings over a finite alphabet with three operations: alternate concatenate (omitable) repeat0ormoretimes Additional operations are available: + repeat1ormoretimes complement w.r.t. an alphabet (or an expression ) difference... (and more)... PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 14 / 62

Review: Regular Expressions RE defines strings over a finite alphabet with three operations: alternate concatenate (omitable) repeat0ormoretimes Additional operations are available: + repeat1ormoretimes complement w.r.t. an alphabet (or an expression ) difference... (and more)... Examples: abc, ab c*, (a b)(c d), ab[~ab]*, a*b*c*-aaa Complement of expression is typically not available in RE tools due to its complexity. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 14 / 62

Specifying Token Patterns with RE Tokens with single lexeme, such as keywords, operators, and delimiters, are easy: Int = "int" Void = "void" PLUS = "+" MINUS = "-"... PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 15 / 62

Specifying Token Patterns with RE Tokens with single lexeme, such as keywords, operators, and delimiters, are easy: Int = "int" Void = "void" PLUS = "+" MINUS = "-"... The ID pattern has one small issue: an ID cannot have the same spelling as a keyword. We can use the difference operation to solve this problem: Digit = [0-9] Letter = [A-Z a-z] KWD = Int Void... ID = Letter (Letter Digit "_")* - KWD Compiler typically uses a different approach. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 15 / 62

Specifying Token Patterns with RE String literal is straightforward: StrLit = "\"" (~["\"","\n"])+ "\"" PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 16 / 62

Specifying Token Patterns with RE String literal is straightforward: StrLit = "\"" (~["\"","\n"])+ "\"" Numerical literals can be a little tricky: For integers, there can be multiple forms: decimal, octal, and hex. For floating-point numbers, both the integral part and the fractional part can be empty, e.g. 123. and.123, yettheycan tbeemptyatthe same time. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 16 / 62

Specifying Token Patterns with RE String literal is straightforward: StrLit = "\"" (~["\"","\n"])+ "\"" Numerical literals can be a little tricky: For integers, there can be multiple forms: decimal, octal, and hex. For floating-point numbers, both the integral part and the fractional part can be empty, e.g. 123. and.123, yettheycan tbeemptyatthe same time. OctLit = 0([0-7])+ HexLit = "0x" ([0-9a-f])+ DecLit = Digit+ - OctLit IntLit = DecLit OctLit HexLit RealLit = Digit+ "." Digit* Digit* "." Digit+ PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 16 / 62

Is RE Perfect for Token Specification? Consider C s non-nesting block comments: A block comment begins with the characters /* and ends with the first subsequent occurrence of the characters */. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 17 / 62

Is RE Perfect for Token Specification? Consider C s non-nesting block comments: A block comment begins with the characters /* and ends with the first subsequent occurrence of the characters */. The following is a direct encoding of the definition in RE: BlockComment = "/*" (~("*/" <EOF>))* "*/"> PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 17 / 62

Is RE Perfect for Token Specification? Consider C s non-nesting block comments: A block comment begins with the characters /* and ends with the first subsequent occurrence of the characters */. The following is a direct encoding of the definition in RE: BlockComment = "/*" (~("*/" <EOF>))* "*/"> It uses the extended complement operator. While theoretically correct, most RE processing systems don t allow it. (Why?) PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 17 / 62

Is RE Perfect for Token Specification? Consider C s non-nesting block comments: A block comment begins with the characters /* and ends with the first subsequent occurrence of the characters */. The following is a direct encoding of the definition in RE: BlockComment = "/*" (~("*/" <EOF>))* "*/"> It uses the extended complement operator. While theoretically correct, most RE processing systems don t allow it. (Why?) It is harder to define BlockComment without using this operator. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 17 / 62

Is RE Perfect for Token Specification? Consider C s non-nesting block comments: A block comment begins with the characters /* and ends with the first subsequent occurrence of the characters */. The following is a direct encoding of the definition in RE: BlockComment = "/*" (~("*/" <EOF>))* "*/"> It uses the extended complement operator. While theoretically correct, most RE processing systems don t allow it. (Why?) It is harder to define BlockComment without using this operator. Now consider the pattern of ML s nesting block comments: Comments in Standard ML begin with (* and end with *). Comments can be nested which means that all (* tags must end with a *) tag. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 17 / 62

Is RE Perfect for Token Specification? Consider C s non-nesting block comments: A block comment begins with the characters /* and ends with the first subsequent occurrence of the characters */. The following is a direct encoding of the definition in RE: BlockComment = "/*" (~("*/" <EOF>))* "*/"> It uses the extended complement operator. While theoretically correct, most RE processing systems don t allow it. (Why?) It is harder to define BlockComment without using this operator. Now consider the pattern of ML s nesting block comments: Comments in Standard ML begin with (* and end with *). Comments can be nested which means that all (* tags must end with a *) tag. Question: Can you write a regular expression for it? PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 17 / 62

Review: Finite Automata For every regular set L, there exists a finite automaton that accepts L. Example: aa bb a ϵ 1 2 start 0 ϵ b 3 4 a b Circles represent states; double-circles represent final states. Labeled edges represent transitions. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 18 / 62

Review: Finite Automata For every regular set L, there exists a finite automaton that accepts L. Example: aa bb a ϵ 1 2 start 0 ϵ b 3 4 a b Circles represent states; double-circles represent final states. Labeled edges represent transitions. A finite automaton accepts string x if there is a path from the start state to a final state labeled by characters of x. A finite automaton accepts language L if it accepts exactly the strings of L. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 18 / 62

NFA vs. DFA RE: aa bb PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 19 / 62

NFA vs. DFA RE: aa bb NFA: start 0 ϵ ϵ 1 a 2 3 b 4 a b Transitions may be labeled by ϵ Multiple transitions from the same state may be labeled by the same symbol PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 19 / 62

NFA vs. DFA RE: aa bb NFA: start 0 ϵ ϵ 1 a 2 3 b 4 a b Transitions may be labeled by ϵ Multiple transitions from the same state may be labeled by the same symbol DFA: start 0 a b 1 2 a b No ϵ-transitions Each state has at most one transition labeled by the same symbol PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 19 / 62

Transition Graph vs. Table Form RE: (a b) abb NFA: a start a b b 0 1 2 3 b State ϵ a b 0 0,1 0 1 2 2 3 3 DFA: b start 2 b a 0 a 1 b a b 3 b 4 a State a b 0 1 2 1 1 3 2 1 2 3 1 4 4 1 2 a PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 20 / 62

Using FA to Recognize Tokens Assume tokens are defined by RE. Steps: 1. Construct an NFA based on the token REs. 2. Convert the NFA to a DFA. 3. (Optional) Minimize the DFA s states. 4. Implement the DFA as a lexer program. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 21 / 62

Using FA to Recognize Tokens Assume tokens are defined by RE. Steps: 1. Construct an NFA based on the token REs. 2. Convert the NFA to a DFA. 3. (Optional) Minimize the DFA s states. 4. Implement the DFA as a lexer program. We ll use (a b) abb as an example. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 21 / 62

Step 1. RE NFA ϵ a 2 3 ϵ start a b: 1 ϵ b 4 5 ϵ 6 ϵ ϵ a 2 3 ϵ (a b) start ϵ : 0 1 ϵ b 4 5 ϵ ϵ 6 7 ϵ start a b b abb: 8 9 10 11 ϵ (a b) start ϵ abb: 0 1 ϵ ϵ 2 a 3 4 b 5 ϵ ϵ ϵ 6 7 ϵ a b b 8 9 10 11 ϵ PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 22 / 62

Step 2. NFA DFA NFA: ϵ ϵ a 2 3 ϵ start ϵ 0 1 ϵ b 4 5 ϵ ϵ 6 7 a b b 8 9 10 ϵ Find the ϵ-closure for the start state 0: start 0 1 2 4 7 Each state in the DFA corresponds to a set of states in the NFA. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 23 / 62

Step 2. NFA DFA (cont.) NFA: ϵ ϵ a 2 3 ϵ start ϵ 0 1 ϵ b 4 5 ϵ ϵ 6 7 a b b 8 9 10 ϵ Find the target states corresponding to symbols a and b: b 1 2 4 5 6 7 start 0 1 2 4 7 a 1 6 2 4 7 3 8 For each symbol, the DFA s transition simulates all possible transitions in the NFA. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 24 / 62

Step 2. NFA DFA (cont.) NFA: ϵ ϵ a 2 3 ϵ start ϵ 0 1 ϵ b 4 5 ϵ ϵ 6 7 a b b 8 9 10 ϵ Repeat the process for the new states: b b 1 2 4 5 6 7 a start 0 1 2 4 7 a 1 6 2 4 7 3 8 b a 1 6 2 5 7 4 9 a PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 25 / 62

Step 2. NFA DFA (cont.) NFA: ϵ ϵ a 2 3 ϵ start ϵ 0 1 ϵ b 4 5 ϵ ϵ 6 7 a b b 8 9 10 ϵ Finally: b 1 2 4 b 5 6 7 a b start 0 1 2 4 7 a 1 6 2 4 7 3 8 b a 1 6 2 5 7 4 9 b 1 6 2 4 5 7 10 a a PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 26 / 62

Step 2. NFA DFA (cont.) The final DFA: b start 2 b a 0 a 1 b a b 3 b 4 a State a b 0 1 2 1 1 3 2 1 2 3 1 4 4 1 2 a PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 27 / 62

Step 4. DFA Lexer Program b start 2 b a 0 a 1 b a b 3 b 4 a State a b 0 1 2 1 1 3 2 1 2 3 1 4 4 1 2 a A DFA implementing token REs can be easily turned into a program that answers yes or no on whether a given input sequence represents a token: Traverse the DFA with the input and see if it reaches a final state. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 28 / 62

Step 4. DFA Lexer Program b start 2 b a 0 a 1 b a b 3 b 4 a State a b 0 1 2 1 1 3 2 1 2 3 1 4 4 1 2 a A DFA implementing token REs can be easily turned into a program that answers yes or no on whether a given input sequence represents a token: Traverse the DFA with the input and see if it reaches a final state. Examples: a b b yes, since it reaches a final state. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 28 / 62

Step 4. DFA Lexer Program b start 2 b a 0 a 1 b a b 3 b 4 a State a b 0 1 2 1 1 3 2 1 2 3 1 4 4 1 2 a A DFA implementing token REs can be easily turned into a program that answers yes or no on whether a given input sequence represents a token: Traverse the DFA with the input and see if it reaches a final state. Examples: a b b yes, since it reaches a final state. b a a no, since it does not reach a final state. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 28 / 62

Step 4. DFA Lexer Program (cont.) b start 2 b a 0 a 1 b a b 3 b 4 a State a b 0 1 2 1 1 3 2 1 2 3 1 4 4 1 2 a However, if the input sequence may represent multiple tokens, there is a problem when should the program report yes? Should it report as soon as the transitions reach a final state? Or should it keep on reading more input before reporting? PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 29 / 62

Step 4. DFA Lexer Program (cont.) b start 2 b a 0 a 1 b a b 3 b 4 a State a b 0 1 2 1 1 3 2 1 2 3 1 4 4 1 2 a However, if the input sequence may represent multiple tokens, there is a problem when should the program report yes? Should it report as soon as the transitions reach a final state? Or should it keep on reading more input before reporting? Examples: a b b a b b one or two tokens? PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 29 / 62

Step 4. DFA Lexer Program (cont.) b start 2 b a 0 a 1 b a b 3 b 4 a State a b 0 1 2 1 1 3 2 1 2 3 1 4 4 1 2 a However, if the input sequence may represent multiple tokens, there is a problem when should the program report yes? Should it report as soon as the transitions reach a final state? Or should it keep on reading more input before reporting? Examples: a b b a b b one or two tokens? a b b a b one token plus some garbage or a single piece of garbage? PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 29 / 62

Longest Match/Maximal Munch Rule A widely used convention for token recognition. If multiple lexemes can be formed from the same input point, the longest one will be chosen. i n t 1 2 3 oneid token PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 30 / 62

Longest Match/Maximal Munch Rule A widely used convention for token recognition. If multiple lexemes can be formed from the same input point, the longest one will be chosen. i n t 1 2 3 oneid token A white space (including tab, newline, and comment, etc.) automatically terminates a lexeme. i n t 1 2 3 oneid token followed by one INTLIT token PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 30 / 62

Longest Match/Maximal Munch Rule A widely used convention for token recognition. If multiple lexemes can be formed from the same input point, the longest one will be chosen. i n t 1 2 3 oneid token A white space (including tab, newline, and comment, etc.) automatically terminates a lexeme. i n t 1 2 3 oneid token followed by one INTLIT token (Priority/First-Match Rule) If the longest lexeme can be matched with multiple tokens, the token whose definition appears first in the token specification file has the priority. b e g i n keywordorid? PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 30 / 62

Longest Match/Maximal Munch Rule A widely used convention for token recognition. If multiple lexemes can be formed from the same input point, the longest one will be chosen. i n t 1 2 3 oneid token A white space (including tab, newline, and comment, etc.) automatically terminates a lexeme. i n t 1 2 3 oneid token followed by one INTLIT token (Priority/First-Match Rule) If the longest lexeme can be matched with multiple tokens, the token whose definition appears first in the token specification file has the priority. b e g i n keywordorid? (Corollary) In a token definition file, always define keywords before ID. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 30 / 62

Longest Match/Maximal Munch Rule Exercises Show token sequence for each of the following Java input: x + + + y x + + + + y x + + + + + y x + + + + + y x + + + + + y x + + + + + y PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 31 / 62

Longest Match/Maximal Munch Rule Exercises Show token sequence for each of the following Java input: x + + + y ID(x) "++" "+" ID(y) x + + + + y x + + + + + y x + + + + + y x + + + + + y x + + + + + y PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 31 / 62

Longest Match/Maximal Munch Rule Exercises Show token sequence for each of the following Java input: x + + + y x + + + + y ID(x) "++" "+" ID(y) ID(x) "++" "++" ID(y) x + + + + + y x + + + + + y x + + + + + y x + + + + + y PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 31 / 62

Longest Match/Maximal Munch Rule Exercises Show token sequence for each of the following Java input: x + + + y x + + + + y x + + + + + y ID(x) "++" "+" ID(y) ID(x) "++" "++" ID(y) ID(x) "++" "++" "+" ID(y) x + + + + + y x + + + + + y x + + + + + y PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 31 / 62

Longest Match/Maximal Munch Rule Exercises Show token sequence for each of the following Java input: x + + + y x + + + + y x + + + + + y ID(x) "++" "+" ID(y) ID(x) "++" "++" ID(y) ID(x) "++" "++" "+" ID(y) x + + + + + y ID(x) "++" "++" "+" ID(y) x + + + + + y x + + + + + y PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 31 / 62

Longest Match/Maximal Munch Rule Exercises Show token sequence for each of the following Java input: x + + + y x + + + + y x + + + + + y ID(x) "++" "+" ID(y) ID(x) "++" "++" ID(y) ID(x) "++" "++" "+" ID(y) x + + + + + y ID(x) "++" "++" "+" ID(y) x + + + + + y ID(x) "++" "+" "++" ID(y) x + + + + + y PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 31 / 62

Longest Match/Maximal Munch Rule Exercises Show token sequence for each of the following Java input: x + + + y x + + + + y x + + + + + y ID(x) "++" "+" ID(y) ID(x) "++" "++" ID(y) ID(x) "++" "++" "+" ID(y) x + + + + + y ID(x) "++" "++" "+" ID(y) x + + + + + y ID(x) "++" "+" "++" ID(y) x + + + + + y ID(x) "+" "+" "+" "++" ID(y) PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 31 / 62

Dealing with Difficult Patterns An alternative way of finding a regular expression for a pattern is to find a finite automata first, then convert the FA to an RE. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 32 / 62

Dealing with Difficult Patterns An alternative way of finding a regular expression for a pattern is to find a finite automata first, then convert the FA to an RE. Example: Find a regular expression for the set of sequences of digits whose values are multiples of 3. E.g. 3, 060, 12345 are all in the set, while 11, 22, 2345 are not. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 32 / 62

Dealing with Difficult Patterns An alternative way of finding a regular expression for a pattern is to find a finite automata first, then convert the FA to an RE. Example: Find a regular expression for the set of sequences of digits whose values are multiples of 3. E.g. 3, 060, 12345 are all in the set, while 11, 22, 2345 are not. Analysis: On the surface, the set seems to be defined through a semantic property, i.e., value of sequence. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 32 / 62

Dealing with Difficult Patterns An alternative way of finding a regular expression for a pattern is to find a finite automata first, then convert the FA to an RE. Example: Find a regular expression for the set of sequences of digits whose values are multiples of 3. E.g. 3, 060, 12345 are all in the set, while 11, 22, 2345 are not. Analysis: On the surface, the set seems to be defined through a semantic property, i.e., value of sequence. However, mathematical knowledge tells us that this particular semantic property can be mapped to an equivalent syntactical property: A sequence of digits whose value is a multiple of 3 The sum of the sequence s digits is a multiple of 3 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 32 / 62

Dealing with Difficult Patterns (cont.) Even with the new insight, it is still not easy to come up with a regular expression. (It s a good exercise to try.) PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 33 / 62

Dealing with Difficult Patterns (cont.) Even with the new insight, it is still not easy to come up with a regular expression. (It s a good exercise to try.) Now, let s think from a finite automaton s point of view. With respect to the property of sequence s value being a multiples of 3, an arbitrary sequence of digits can only be in one of three states: S1. It s value is a multiple of 3 (so is the sum of its digits) S2. It s value is a multiple of 3 plus 1 S3. It s value is a multiple of 3 plus 2 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 33 / 62

Dealing with Difficult Patterns (cont.) With this info, we can define the following DFA for recognizing the set: 0,3,6,9 0,3,6,9 start 0,3,6,9 0 1 2,5,8 2 2,5,8 1,4,7 1,4,7 2,5,8 0,3,6,9 3 1,4,7 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 34 / 62

Dealing with Difficult Patterns (cont.) With this info, we can define the following DFA for recognizing the set: 0,3,6,9 0,3,6,9 start 0,3,6,9 0 1 2,5,8 2 2,5,8 1,4,7 1,4,7 2,5,8 0,3,6,9 3 1,4,7 If the goal is to implement a lexer, we can go directly from here. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 34 / 62

Dealing with Difficult Patterns (cont.) With this info, we can define the following DFA for recognizing the set: 0,3,6,9 0,3,6,9 start 0,3,6,9 0 1 2,5,8 2 2,5,8 1,4,7 1,4,7 2,5,8 0,3,6,9 3 1,4,7 If the goal is to implement a lexer, we can go directly from here. If the goal is to find a RE, we can follow an conversion algorithm. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 34 / 62

Converting a DFA to a RE Method: The state removal method. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 35 / 62

Converting a DFA to a RE Method: The state removal method. Step 1. (Preparation) If the start state has an incoming edge from another state, create a new start state with an ϵ transition connecting to the original start state. If there are multiple final states, or a final state with an out-going edge, create a new final state with ϵ transitions connecting from all the original final states. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 35 / 62

Converting a DFA to a RE Method: The state removal method. Step 1. (Preparation) If the start state has an incoming edge from another state, create a new start state with an ϵ transition connecting to the original start state. If there are multiple final states, or a final state with an out-going edge, create a new final state with ϵ transitions connecting from all the original final states. Step 2. (Elimination) Pick a state. For every pair of incoming and outgoing edges (excluding self-loop), consolidate the labels on the path to a single label, and create a direct edge with that label. Eliminate the state. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 35 / 62

Converting a DFA to a RE Method: The state removal method. Step 1. (Preparation) If the start state has an incoming edge from another state, create a new start state with an ϵ transition connecting to the original start state. If there are multiple final states, or a final state with an out-going edge, create a new final state with ϵ transitions connecting from all the original final states. Step 2. (Elimination) Pick a state. For every pair of incoming and outgoing edges (excluding self-loop), consolidate the labels on the path to a single label, and create a direct edge with that label. Eliminate the state. Repeat this step until done. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 35 / 62

Converting a DFA to a RE 1. Preparation. L 0 =0,3,6,9 L 1 =1,4,7 L 2 =2,5,8 L 0 L 2 2 L 2 L 1 L 1 start L 0 0 L 1 2 L 0 L 0 3 L 1 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 36 / 62

Converting a DFA to a RE 1. Preparation. L 0 =0,3,6,9 L 1 =1,4,7 L 2 =2,5,8 L 0 L 2 2 L 2 L 1 L 1 start L 0 0 L 1 2 L 0 L 0 3 L 1 L 0 1 L 0 L 2 2 L 2 ϵ L 1 L 1 start L 0 0 L 1 2 L 0 3 L 1 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 36 / 62

Converting a DFA to a RE 2. Eliminating State 2. L 0 1 L 0 L 2 2 L 2 ϵ L 1 L 1 start L 0 0 L 1 2 L 0 3 L 1 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 37 / 62

Converting a DFA to a RE 2. Eliminating State 2. L 0 1 L 0 L 2 2 L 2 ϵ L 1 L 1 start L 0 0 L 1 2 L 0 3 L 1 1 L 0 +L 1 L 0 *L 2 L 0 +L 2 L 0 *L 1 ϵ start L 0 0 L 1 2 +L 1 L 0 *L 1 3 L 1 +L 2 L 0 *L 2 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 37 / 62

Converting a DFA to a RE 3. Eliminating State 3. 1 L 0 +L 1 L 0 *L 2 L 0 +L 2 L 0 *L 1 ϵ start L 0 0 L 1 2 +L 1 L 0 *L 1 3 L 1 +L 2 L 0 *L 2 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 38 / 62

Converting a DFA to a RE 3. Eliminating State 3. 1 L 0 +L 1 L 0 *L 2 L 0 +L 2 L 0 *L 1 ϵ start L 0 0 L 1 2 +L 1 L 0 *L 1 3 L 1 +L 2 L 0 *L 2 1 L 0 +L 1 L 0 *L 2 +(L 2 +L 1 L 0 *L 1 )(L 0 +L 2 L 0 *L 1 )*(L 1 +L 2 L 0 *L 2 ) ϵ start L 0 0 1 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 38 / 62

Converting a DFA to a RE 4. Eliminating State 1. 1 L 0 +L 1 L 0 *L 2 +(L 2 +L 1 L 0 *L 1 )(L 0 +L 2 L 0 *L 1 )*(L 1 +L 2 L 0 *L 2 ) ϵ start L 0 0 1 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 39 / 62

Converting a DFA to a RE 4. Eliminating State 1. 1 L 0 +L 1 L 0 *L 2 +(L 2 +L 1 L 0 *L 1 )(L 0 +L 2 L 0 *L 1 )*(L 1 +L 2 L 0 *L 2 ) ϵ start L 0 0 1 1 start 0 L 0 (L 0 +L 1 L 0 *L 2 +(L 2 +L 1 L 0 *L 1 )(L 0 +L 2 L 0 *L 1 )*(L 1 +L 2 L 0 *L 2 ))* L 0 =0,3,6,9 L 1 =1,4,7 L 2 =2,5,8 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 39 / 62

Context-Free Grammars (BNF Notation) BNF = Backus-Naur Form Common convention: Nonterminals begin with upper-case letters. Literal terminals are quoted. Other terminals begin with lower-case letters. Program begin StmtList end StmtList Statement StmtList StmtList Statement Statement Assignment Statement ReadStmt Statement WriteStmt Assignment id := Expression ; ReadStmt read ( IdList ) ; WriteStmt write ( ExprList ) ; IdList id, IdList IdList id ExprList Expression, ExprList ExprList Expression Expression Primary + Expression Expression Primary Expression Expression Primary Primary ( Expression ) Primary id Primary integer PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 40 / 62

Context-Free Grammars (EBNF Notation) EBNF = Extended BNF Common convention: for selection (,) for grouping [ ] for an optional construct {} for zero or more repetitions Program begin StmtList end StmtList Statement {Statement} Statement Assignment ReadStmt WriteStmt Assignment id := Expression ; ReadStmt read ( IdList ) ; WriteStmt write ( ExprList ) ; IdList id {, id } ExprList Expression {, Expression} Expression Primary {( + ) Primary} Primary ( Expression ) id integer PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 41 / 62

Equivalence Between EBNF and BNF Extended BNF does not have extra power over BNF. In fact, any grammar in EBNF can be transformed into one in proper BNF: A α [ β ] γ A αbγ B β B ϵ A α {β} γ A αbγ B βb B ϵ PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 42 / 62

RE vs. CFG Every set that can be described by a regular expression can also be described by a context-free grammar. Example: The RE (a b) abb can be described by the CFG a A 0 aa 0 ba 0 aa 1 A 1 ba 2 A 2 ba 3 A 3 ϵ start a b b 0 1 2 3 b Regular grammars are just CFGs with their productions limited to the following two forms, A ab and C c PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 43 / 62

Derivations A derivation step is to replace the lhs of a production by its rhs. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 44 / 62

Derivations A derivation step is to replace the lhs of a production by its rhs. Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 44 / 62

Derivations A derivation step is to replace the lhs of a production by its rhs. Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 44 / 62

Derivations A derivation step is to replace the lhs of a production by its rhs. Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 44 / 62

Derivations A derivation step is to replace the lhs of a production by its rhs. Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 3 T P+T 2 T +T PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 44 / 62

Derivations A derivation step is to replace the lhs of a production by its rhs. Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T 3 T P+T 4 P P+T PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 44 / 62

Derivations A derivation step is to replace the lhs of a production by its rhs. Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 44 / 62

Derivations A derivation step is to replace the lhs of a production by its rhs. Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 44 / 62

Derivations A derivation step is to replace the lhs of a production by its rhs. Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 44 / 62

Derivations A derivation step is to replace the lhs of a production by its rhs. Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 44 / 62

Derivations A derivation step is to replace the lhs of a production by its rhs. Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id The set of sentences derived from the start symbol S of a grammar G defines the language of the grammar, denoted L(G): L(G) ={x V t S + x}. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 44 / 62

Derivations A derivation step is to replace the lhs of a production by its rhs. Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id The set of sentences derived from the start symbol S of a grammar G defines the language of the grammar, denoted L(G): L(G) ={x V t S + x}. Note: Derivations are defined with respect to proper productions. They don t work with grammars in EBNF form. While the rules in an EBNF grammar are of the form lhs rhs, theyarenotproperproductions. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 44 / 62

Parse Tree A.k.a. concrete syntax tree A tree representation of complete derivations, i.e., from the start symbol to a sentence. PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 45 / 62

Parse Tree A.k.a. concrete syntax tree A tree representation of complete derivations, i.e., from the start symbol to a sentence. 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id E root node start symbol E + T leaf nodes terminals T P non-leaf nodes nonterminals non-leaf nodes with children productions T * P id3 P id2 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 45 / 62 id1

Multiple Derivations The same sentence often can be derived by different sequences of productions: Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 46 / 62

Multiple Derivations The same sentence often can be derived by different sequences of productions: Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id Three different derivations: 1. E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 46 / 62

Multiple Derivations The same sentence often can be derived by different sequences of productions: Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id Three different derivations: 1. E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id 2. E 1 E +T 2 T +T 3 T P+T 4 P P+T 4 P P+P 5 P P +id 5 P id + id 5 id id + id PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 46 / 62

Multiple Derivations The same sentence often can be derived by different sequences of productions: Example: 1. E E+T 2. E T 3. T T*P 4. T P 5. P id Three different derivations: 1. E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id 2. E 1 E +T 2 T +T 3 T P+T 4 P P+T 4 P P+P 5 P P +id 5 P id + id 5 id id + id 3. E 1 E+T 4 E+P 5 E +id 2 T +id 3 T P+id 4 P P+id 5 id P +id 5 id id + id PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 46 / 62

Multiple Derivations For unambiguous grammars, different derivations correspond to different ways of building up the same parse tree. 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E E + T E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 47 / 62

Multiple Derivations For unambiguous grammars, different derivations correspond to different ways of building up the same parse tree. 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E E + T T E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 47 / 62

Multiple Derivations For unambiguous grammars, different derivations correspond to different ways of building up the same parse tree. 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id E E + T T T * P PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 47 / 62

Multiple Derivations For unambiguous grammars, different derivations correspond to different ways of building up the same parse tree. 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id E E + T T T * P P PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 47 / 62

Multiple Derivations For unambiguous grammars, different derivations correspond to different ways of building up the same parse tree. 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id E E + T T T * P P id1 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 47 / 62

Multiple Derivations For unambiguous grammars, different derivations correspond to different ways of building up the same parse tree. 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id E E + T T T * P P id2 id1 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 47 / 62

Multiple Derivations For unambiguous grammars, different derivations correspond to different ways of building up the same parse tree. 1. E E+T 2. E T 3. T T*P 4. T P 5. P id E 1 E +T 2 T +T 3 T P+T 4 P P+T 5 id P +T 5 id id + T 4 id id + P 5 id id + id E E + T T P T * P P id2 id1 PSU CS320 Fall 17 Week 2: Syntax Specification, Grammars 47 / 62