Syntax (2).

Slides:



Advertisements
Similar presentations
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
Advertisements

Chapter 2 Syntax. Syntax The syntax of a programming language specifies the structure of the language The lexical structure specifies how words can be.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Programming Languages 2nd edition Tucker and Noonan
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 10 Function Implementation In theory, there.
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Chapter 2: Syntax Fall 2009 Marco.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 3 Lexical and Syntactic Analysis Syntactic.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 3 Lexical and Syntactic Analysis Syntactic.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are.
Building lexical and syntactic analyzers
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Syntax (Slides mainly based on Tucker.
2440: 211 Interactive Web Programming Expressions & Operators.
Syntax – Intro and Overview CS331. Syntax Syntax defines what is grammatically valid in a programming language –Set of grammatical rules –E.g. in English,
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Dr. Philip Cannata 1 Lexical and Syntactic Analysis Chomsky Grammar Hierarchy Lexical Analysis – Tokenizing Syntactic Analysis – Parsing Hmm Concrete Syntax.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Recognizing PL/SQL Lexical Units. 2 home back first prev next last What Will I Learn? List and define the different types of lexical units available in.
Introduction to Programming Languages S1.3.1Bina © 1998 Liran & Ofir Introduction to Programming Languages Programming in C.
CPS 506 Comparative Programming Languages Syntax Specification.
CSCE 330 Programming Language Structures Syntax (Slides mainly based on Tucker and Noonan) Fall 2012 A language that is simple to parse for the compiler.
Syntax and Semantics Structure of programming languages.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
Language Implementation Overview John Keyser Spring 2016.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
Structure of programming languages
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
Chapter 2: Basic Elements of C++
Chapter 3 – Describing Syntax
CSCE 330 Programming Language Structures Syntax (Slides mainly based on Tucker and Noonan) Fall 2012 A language that is simple to parse for the compiler.
Programming Languages 2nd edition Tucker and Noonan
Semantic Analysis Chapter 4.
A Simple Syntax-Directed Translator
Constructing Precedence Table
Tutorial On Lex & Yacc.
Syntax (1).
Data Types, Identifiers, and Expressions
Java Programming: From Problem Analysis to Program Design, 4e
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
2.1 Parts of a C++ Program.
Programming Languages 2nd edition Tucker and Noonan
C H A P T E R T W O Syntax.
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Programming Languages 2nd edition Tucker and Noonan
The Data Element.
The Data Element.
Lexical Elements & Operators
Faculty of Computer Science and Information System
Presentation transcript:

Syntax (2)

Clite Grammar (1) lexical level 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 ‘

Issues Not Addressed by this Grammar Comments Whitespace Distinguishing one token <= from two tokens < = Distinguishing identifiers from keywords like if

Lexical Syntax (Lexer ) Input: a stream of characters from the ASCII set, keyed by a programmer. Output: a stream of tokens

Classes of Tokens Identifiers: Stack, x, i, push Literals : 123, 'x', 3.25, true Keywords : bool false true char int float if else while main Operators : = || && == != < <= > >= + - * / ! Punctuation : ; , { } ( )

Whitespace Whitespace is any: No token may contain embedded whitespace Tab end-of-line character (or characters) character sequence inside a comment No token may contain embedded whitespace (unless it is a character or string literal) Example: >= one token > = two tokens

Whitespace Examples in Pascal while a < b do legal - spacing between tokens whilea < b do Whilea – valid identifier token whilea < -invalid statement prefix

Keywords and Identifiers Both an identifier and a keyword are lexically the same. if is a keyword and it is also an identifier In most languages keywords are reserved and cannot be used as identifiers main in C and C++ is not a reserved word but it is special identifier

Integer and float values Range and storage Not limited in Clite grammar No idea about storage both limits and storage space are semantic issues

Concrete Syntax (Parser) Based on BNF/EBNF grammar Input: tokens Output: Concrete Syntax (parse) tree or Abstract Syntax tree

Concrete Syntax of Clite Metabraces {} imply left associativity Metabrackets [] makes EquOp and RelOp non-associative In C++, the expression: if (a < x < b) is not equivalent to if (a < x && x < b) But it is error-free! Clite Differs from C/C++ Fewer operators Equality and relational are non-associative

Clite Grammar (2) Expressions Operator Associativity Unary - ! none * / left + - left < <= > >= none == != none && left || left 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 | Literal | ( Expression ) | Type ( Expression )

Clite Grammar (3) Statements Program  int main ( ) { Declarations Statements } Declarations  { Declaration } Declaration  Type Identifier { , Identifier } Type  int | bool | float | char Statements  { Statement } Statement  ; | Block | Assignment | IfStatement | WhileStatement Block  { Statements } Assignment  Identifier = Expression ; IfStatement  if ( Expression ) Statement [ else Statement ] WhileStatement  while ( Expression ) Statement

Abstract Syntax Removes “syntactic redundancies ” and keeps essential elements of a language. Pascal while i < n do begin i := i + 1; end; C/C++ while (i < n) { i = i + 1; } The only essential information It is a loop A terminating condition i < n A body increments the current value of i.

Parse and Abstract Syntax trees Parse tree is inefficient The shape of the parse tree reveals the meaning of the program. So we want a tree that removes its inefficiency and keeps its shape. Remove separator/punctuation terminal symbols Remove all trivial root nonterminals Replace remaining nonterminals with leaf terminals

Example: z = x + 2*y; Parse Tree Abstract Syntax Tree

Partial Abstract Syntax of Clite Assignment = Variable target; Expression source Expression = Variable | Value | Binary | Unary Variable = String id Value = Integer Value Binary = Operator op; Expression term1, term2 Unary = Operator op; Expression term Operator = + | - | * | / | !

Example Abstract Syntax Tree for z = x+2*y Assignment = Variable target; Expression source Expression = Variable | Value | Binary | Unary Variable = String id Value = Integer Value Binary = Operator op; Expression term1, term2 Unary = Operator op; Expression term Operator = + | - | * | / | ! Assignment Variable Binary Operator Value z + * x y 2