Syntax. A. Bellaachia Page: 1

Similar documents
Syntax. In Text: Chapter 3

Chapter 4. Syntax - the form or structure of the expressions, statements, and program units

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

CPS 506 Comparative Programming Languages. Syntax Specification

3. Context-free grammars & parsing

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

Programming Language Definition. Regular Expressions

Chapter 3. Describing Syntax and Semantics ISBN

Chapter 3. Describing Syntax and Semantics

Describing Syntax and Semantics

COP 3402 Systems Software Syntax Analysis (Parser)

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

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

Programming Language Syntax and Analysis

Dr. D.M. Akbar Hussain

CSE 3302 Programming Languages Lecture 2: Syntax

This book is licensed under a Creative Commons Attribution 3.0 License

3. DESCRIBING SYNTAX AND SEMANTICS

Chapter 3. Describing Syntax and Semantics

ICOM 4036 Spring 2004

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

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

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Chapter 3. Describing Syntax and Semantics ISBN

EECS 6083 Intro to Parsing Context Free Grammars

CSCI312 Principles of Programming Languages!

Habanero Extreme Scale Software Research Project

Informatica 3 Syntax and Semantics

Principles of Programming Languages COMP251: Syntax and Grammars

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Chapter 3. Describing Syntax and Semantics

Programming Languages

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Formal Languages. Formal Languages

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Part 5 Program Analysis Principles and Techniques

CMSC 330: Organization of Programming Languages. Context Free Grammars

Principles of Programming Languages COMP251: Syntax and Grammars

Chapter 3. Topics. Languages. Formal Definition of Languages. BNF and Context-Free Grammars. Grammar 2/4/2019

Week 2: Syntax Specification, Grammars

Syntax and Semantics

CSCE 314 Programming Languages

Project 2 Interpreter for Snail. 2 The Snail Programming Language

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

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

CSE302: Compiler Design

A programming language requires two major definitions A simple one pass compiler

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

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

A Simple Syntax-Directed Translator

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

A simple syntax-directed

1 Lexical Considerations

Syntax Intro and Overview. Syntax

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

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

Lexical Considerations

Lexical Considerations

CMSC 330: Organization of Programming Languages. Context Free Grammars

LECTURE 3. Compiler Phases

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

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

CS323 Lecture - Specifying Syntax and Semantics Last revised 1/16/09

CMSC 330: Organization of Programming Languages

Related Course Objec6ves

CMPT 755 Compilers. Anoop Sarkar.

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

Context-free grammars (CFG s)

CMSC 330: Organization of Programming Languages. Context Free Grammars

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Syntax and Grammars 1 / 21

CMSC 330: Organization of Programming Languages

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

More Assigned Reading and Exercises on Syntax (for Exam 2)

Theory and Compiling COMP360

Lecture 4: Syntax Specification

Defining syntax using CFGs

ECE251 Midterm practice questions, Fall 2010

CMSC 330: Organization of Programming Languages

ffl assign The assign has the form: identifier = expression; For example, this is a valid assign : var1 = 20-3*2; In the assign the identifier gets th

Lexical Scanning COMP360

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

The SPL Programming Language Reference Manual

4. Semantic Processing and Attributed Grammars

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

Optimizing Finite Automata

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

Syntax. 2.1 Terminology

DEMO A Language for Practice Implementation Comp 506, Spring 2018

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

CS558 Programming Languages. Winter 2013 Lecture 1

High Level Languages. Java (Object Oriented) This Course. Jython in Java. Relation. ASP RDF (Horn Clause Deduction, Semantic Web) Dr.

CSE 582 Autumn 2002 Exam 11/26/02

Decaf Language Reference Manual

Transcription:

Syntax 1. Objectives & Definitions... 2 2. Definitions... 3 3. Lexical Rules... 4 4. BNF: Formal Syntactic rules... 6 5. Syntax Diagrams... 9 6. EBNF: Extended BNF... 10 7. Example:... 11 8. BNF Statement Derivation... 12 9. Parse Trees... 13 10. Ambiguity... 14 11. Abstract Syntax-Concrete Syntax... 18 A. Bellaachia Page: 1

1. Objectives & Definitions A programming language is a formal notation for describing algorithm by computer using two components: Syntax: What does a program look like? Semantics: What does a program mean? Who must use language definitions? Other language designers Implementers Programmers (the users of the language) Syntax: It is a set of rules that formally describe the form or structure of the expressions, statements, and program units. Example: o The syntax in programming language define the notation used for arithmetic expressions: Infix notation 5 + 6 Prefix notation + 5 6 o The syntax define the delimiter of a statement: ; Semantics: It is the meaning of any syntactically valid program written in the language, e.g., expressions, statements, and program units. Note that all syntactically correct programs have valid. A. Bellaachia Page: 2

It is hard to describe the meaning of a language: o Informal description: describe in English the meaning of a construct. o Formal description: A mathematical model using formal notation to describe each construct. Example: o The syntax of a C if statement is: If (<expression>) <statement> The meaning of this statement form is that the current value of the expression is true, the embedded statement is selected for execution. o It would be nice to have a formal description for such construct!! 2. Definitions Syntax: o Any language whether natural (such as English) or artificial (such as Java) is a set of words of characters from some alphabet. o A sequence of words is called a sentence (in English) or a statement (in a programming language). o The syntax rules of a language specify which sentences are in the language. o There are two types of syntax rules: Lexical rules Syntactic rules A. Bellaachia Page: 3

3. Lexical Rules o Objectives: It helps the programmer know how to write a syntactically correct program. Compiler developers use syntax rules to write syntax analyzer or parser to check the validity of a program. Availability of tools to generate lexical and syntax analyzers: Lex and Yacc. o Alphabet: They specify the set of characters that constitute the alphabet of the language. An alphabet (or vocabulary), V, is a finite nonempty set of symbols. Example: o <> and # is valid operator in Pascal and not in C. o Words: The way the characters are combined to form a word. A word over V is a finite string of symbols from V. V * is the set of all the words over V. V * is called the closure of V. Example: o Java and java are two different variables in C language. o Languages: A language, L, is any subset of V *. A set of rules for forming the words in a language is called a grammar. A. Bellaachia Page: 4

o Syntactic Elements of a language: Character set Identifiers Operator symbols (e.g., ==,!=, #) Reserved Keywords (e.g., class, For, procedure) Comments Blanks (should not have a space between += in java) Delimiters and Brackets: o ; marks the end of a statement in C and Java. o begin end marks the beginning and the end of a block of code in Ada and Pascal o { } marks the beginning and the end of a block in Java and C. Expressions Statements o Example: A C Program #include <stdio.h> #include <ctype.h> Delimiters void main(){ int i,j; printf (" Type a single digit to be squared: "); scanf ("%d",&i); if (!isdigit(i)){ j = i * i; printf (" %d squared is %d ", i, j); } else printf("your input was not a number!!"); } A. Bellaachia Page: 5

4. BNF: Formal Syntactic rules A little of history: Fortran was defined using informal definitions (English Description) Algol 60 was defined by a formal definition (a context free grammar) developed by John Backus. Meta-Language: It is a language that is used to describe other languages. They may be notational or graphical Example: Backus-Naur Form (BNF) BNF Definition: A formal definition defined by John Bachus and Peter Naur to express Algol syntax. A set of nonterminals, terminals, and production rules which define legal sentences in a language. A BNF Rule: reads as <program> ::= {<stmt>*} program is defined as zero or several statements. A. Bellaachia Page: 6

Another example: Description of while statement <while_stmt> ::= while ( <logic_expr> ) <stmt> Terminals: o The primitive tokens of the language ("a", "+", "begin",...) Nonterminals: o Enclosed in "<" and ">", such as <prog> Production rules: o A single nonterminal, followed by "::=", followed by a sequence of terminals and nonterminals. MetaSymbols: o + : One of more occurrences of the previous element. o * : Zero or more occurrences of the previous element. o : means or Example: Terminals: "a" "b" "c" "+" "-" ";" "begin" "end" Nonterminals <prog> <stmt_list> <stmt> <var> <exp> A. Bellaachia Page: 7

Production rules: <prog> ::= "begin" <stmt_list> "end" <stmt_list> ::= <stmt> <stmt_list> ::= <stmt> ";" <stmt_list> <stmt> ::= <var> ":=" <exp> <var> ::= "a" <var> ::= "b" <var> ::= "c" <exp> ::= <var> "+" <var> <exp> ::= <var> "-" <var> <exp> ::= <var> A. Bellaachia Page: 8

5. Syntax Diagrams A pictorial representation of the syntax of a language. They are equivalent to BNF: Put the terminals in circles or ellipses Put the nonterminals in rectangles Connect with lines with arrowheads Direct mapping to/from EBNF: Nonterminal Terminal Y X Y X Sequence Y1 Y2 Y1 Y2 Alternation Optional Repetition Y1 Y2 Y3 [Y] {Y} Y1 Y2 Y3 Y Y A. Bellaachia Page: 9

Example: Conditional Statement: BNF: <cond> ::= if <expr> {<stmt>+} if <expr> {<stmt>+} else {<stmt>+} if expr { stmt } else { stmt } 6. EBNF: Extended BNF EBNF extends BNF syntax to make grammars more readable. EBNF Symbols: Replace ::= with No < > around nonterminals Enclose terminals in single quotes Optional constructs in [ ] Repetitions in { } (or use ) Grouping with ( ) Sequence: A. Bellaachia Page: 10

if "if " test "then" stmt Optional: [ ] if "if " test "then" stmt ["else" stmt] Alternative: number integer real Group: ( ) exp var ( var "+" var ) Repetition: { } ident_list ident { "," ident} 7. Example: BNF <exp> ::= <exp>+<term> <exp>-<term> <term> <term> ::= <term>*<factor> <term>/<factor> <factor> <factor> ::= (<exp>) <identifier> EBNF exp term { ( + - ) term } term factor { ( * / ) factor } factor ( exp ) identifier A. Bellaachia Page: 11

8. BNF Statement Derivation Statements are generated using a sequence of applications of syntactic rules. A derivation is the process that allows the generation of a statement. Example: A Simple assignment Statements assign id := expr id A B C expr id + expr id * expr ( expr ) id Derivation: A := B * (A + C) assign id := expr A := expr A := id * expr A := B * expr A := B * ( expr ) A := B * ( id + expr ) A := B * ( A + expr ) A := B * ( A + id ) A := B * ( A + C ) A. Bellaachia Page: 12

9. Parse Trees Alternative representation for a derivation Example: A parse tree for the previous example <assign> <id> := <expr> A <id> * <expr> B ( <expr> ) <id> + <expr> A <id> C A. Bellaachia Page: 13

10. Ambiguity A Syntax definition that generates a statement for which there are two or more distinct parse trees or derivations is said to be ambiguous. Example: A classical example of the Dangling else : if (x <0) if (y<0) y = y-1; else y = 0; Which if statement has the else part? if (x <0) if (y<0) y = y-1; else y = 0; if (x <0) if (y<0) y = y-1; else y = 0; Example: Another Simple assignment Statements <assign> <id> := <expr> <id> A B C <expr> <expr> + <expr> <expr> * <expr> ( <expr> ) <id> What is the difference between this syntax and the previous one? A. Bellaachia Page: 14

Two parse trees for A := B + C * A <assign> <id> := <expr> A <expr> * <expr> <expr> + <expr> <id> <id> <id> A B C A. Bellaachia Page: 15

<assign> <id> := <expr> A <expr> + <expr> <id> <expr> * <expr> <id> <id> B C A A. Bellaachia Page: 16

A. Bellaachia Page: 17

11. Abstract Syntax-Concrete Syntax Definition: Two syntax rules have the same abstract syntax if they only differ at the lexical level or concrete syntax. Readability might be affected: Is it better to use!= or Example: For Loop construct: Ada: C: FOR counter IN lowbound..highbound LOOP Sequence of statements; END LOOP; For (i=0;i<cond;cond){ Sequence of statements; } A. Bellaachia Page: 18