Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How.

Slides:



Advertisements
Similar presentations
Defining Program Syntax Chapter TwoModern Programming Languages, 2nd ed.1.
Advertisements

Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
ISBN Chapter 3 Describing Syntax and Semantics.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
PZ02A - Language translation
Chapter 3 Describing Syntax and Semantics Sections 1-3.
1 CSE305 Programming Languages Syntax What is it? How is it specified? Who uses it? Why is it needed?
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 2-a Defining Program Syntax
Programming Languages An Introduction to Grammars Oct 18th 2002.
1 Introduction: syntax and semantics Syntax: a formal description of the structure of programs in a given language. Semantics: a formal description of.
30-Jun-15 BNF. Metalanguages A metalanguage is a language used to talk about a language (usually a different one) We can use English as its own metalanguage.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Chapter 3: Formal Translation Models
COP4020 Programming Languages
S YNTAX. Outline Programming Language Specification Lexical Structure of PLs Syntactic Structure of PLs Context-Free Grammar / BNF Parse Trees Abstract.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
Chapter TwoModern Programming Languages1 Defining Program Syntax.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Chapter TwoModern Programming Languages1 Defining Program Syntax.
Chpater 3. Outline The definition of Syntax The Definition of Semantic Most Common Methods of Describing Syntax.
Software II: Principles of Programming Languages
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
CSI 3120, Grammars, page 1 Language description methods Major topics in this part of the course: –Syntax and semantics –Grammars –Axiomatic semantics (next.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Syntax Specification and BNF © Allan C. Milne Abertay University v
Syntax and Backus Naur Form
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
A sentence (S) is composed of a noun phrase (NP) and a verb phrase (VP). A noun phrase may be composed of a determiner (D/DET) and a noun (N). A noun phrase.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Grammars CPSC 5135.
ProgrammingLanguages Programming Languages Language Syntax This lecture introduces the the lexical structure of programming languages; the context-free.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
Copyright © by Curt Hill Grammar Types The Chomsky Hierarchy BNF and Derivation Trees.
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
CPS 506 Comparative Programming Languages Syntax Specification.
D Goforth COSC Translating High Level Languages.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
Chapter 3 Describing Syntax and Semantics
Syntax The Structure of a Language. Lexical Structure The structure of the tokens of a programming language The scanner takes a sequence of characters.
1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
ISBN Chapter 3 Describing Syntax and Semantics.
Syntax and Grammars.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
C H A P T E R T W O Syntax and Semantic. 2 Introduction Who must use language definitions? Other language designers Implementors Programmers (the users.
©SoftMoore ConsultingSlide 1 Context-Free Grammars.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
BNF A CFL Metalanguage Some Variations Particular View to SLK Copyright © 2015 – Curt Hill.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Chapter 3 – Describing Syntax
Chapter 3 – Describing Syntax
Automata and Languages What do these have in common?
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
CSE 3302 Programming Languages
CS 3304 Comparative Languages
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
High-Level Programming Language
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
COMPILER CONSTRUCTION
Presentation transcript:

Syntax: 10/18/2015IT 3271 Semantics: Describe the structures of programs Describe the meaning of programs Programming Languages (formal languages) -- How to describe them? -- How to use them? (machine and human) Grammars --Ambiguous (sometimes) Textbook, manuals --Confusing (always) solution: denotation semantics (for nuts only) solution: using unambiguous only

English Grammar The man hit the ball. subject verb object 10/18/2015IT 3272 The man saw the girl with a telescope. subject verb object The purpose of grammar: To have a device to generate all valid sentences in the target language (from a root). To tell whether a sentence is valid. Chomsky: (old fashion)

Noam Chomsky /18/2015IT Syntactic Structures (1957) Generative Grammar A valid sentence is generated from a root according to some fixed rules (grammar).

A generative grammar in Syntactic Structures 10/18/2015IT 3274 S NP VP TN Verb the | a man | ball | car hit | take | took | run | ran NP T N + VP Verb + + ….. root terminal symbols non-terminal symbols

Syntactic Structures 10/18/2015IT 3275 S NP VP TN Verb theman theball hit NP TN the man hit the ball

Backus-Naur Form, BNF 10/18/2015IT 3276 ::= ::= the ::= man | ball ::= hit | took ::= ::= loves | hates|eats ::= a | the ::= dog | cat | rat Grammar 1 Grammar 2 ::= | ::= loves | hates|eats |hit | took ::= a | the ::= the ::= dog | cat | rat|man | ball

Deviation: the sequence of processes that generate a sentence 10/18/2015IT 3277 <S><S> the the man the man hit the man hit the the man hit the ball ::= ::= the ::= man | ball := hit | took Grammar 1 the man hit the ball

10/18/2015IT 3278 Parse: v. To break (a sentence) down into its component parts of speech with an explanation of the form, function, and syntactical relationship of each part. (American Heritage Dict.) the dog loves the cat the loves dog the cat loves the dog the cat × ×

A Parse Tree 10/18/2015IT 3279 <S><S> the dog the cat loves Grammar ::= ::= loves | hates|eats ::= a | the ::= dog | cat | rat “the loves dog the cat” doesn’t have a parse tree

A grammar for Arithmetic Expression 10/18/2015IT ::= + | * | ( ) | a | b | c Example: ((a+b)*c) Is this expression valid? ( ) ( * ) (( ) * ) (( + ) * ) (( a + ) * ) (( a + b ) * ) ((a+b)*c) Yes

A Parse Tree for ((a+b)*c) 10/18/2015IT ( ) * ( ) a b c

Parse Trees for a+b*c 10/18/2015IT * ab c * + bc a ? What is the meaning of a+b*c

Restrictions on Grammars 10/18/2015IT Unrestricted Grammars (type-0) Why context sensitive grammars have less restrictions than context free grammars? Right/Left Linear Grammars (type-3) Context Sensitive (type-1) Context Free (type-2) Diagram in terms of the sizes of the set of restrictions

Chomsky Hierarchy 10/18/2015IT Regular Expressions (type-3) Computable (formal) languages (type-0) Context-free languages (type-2) Context-sensitive languages (type-1) Diagram in terms of the sizes of the language families

A BNF grammar consists of four parts: –The finite set of tokens (terminal symbols) –The finite set of non-terminal symbols –The start symbol –The finite set of production rules 10/18/2015IT ::= ::= the ::= man | ball ::= hit | took Grammars in BNF (Backus-Naur Form)

Constructing Grammars Using divide and conquer to simplify the job. Data types, variable names (identifiers) One variable, one type (this is not grammar’s job to make sure) 10/18/2015IT float a; boolean a, b, c; int a, b;

Primitive type names Using divide and conquer 10/18/2015IT ::= ; ::= boolean | byte | short | int | long | char | float | double ::= |, ::= | =

Tokens: How is such a program file (a sequence of characters) divided into a sequence of tokens? 10/18/2015IT e.g. identifiers ( const, x, fact ) keywords ( if, const ) operators ( == ) constants ( ), etc. Programs stored in files are just sequences of characters, but we want to prepare them into tokens before further analysis. Reserved words Tokens are atoms of the program

Lexical Structure And Phrase Structure Grammars so far have defined phrase structure: how a program is built from a sequence of tokens We also need to define lexical structure: how a text file is divided into tokens 10/18/2015IT 32719

Separate Grammars Usually there are two separate grammars –to construct a sequence of tokens from a file of characters ( Lexical Structure) –to construct a parse tree from a sequence of tokens ( Phrase Structure) 10/18/2015IT ::= | ::= | | ::= | | ::= | | | …

Separate Compiler Passes Scanner  tokens string parser  parse tree (more to do afterwards) 10/18/2015IT 32721

Historical Note #1 Early languages sometimes did not separate lexical structure from phrase structure –Early Fortran and Algol dialects allowed spaces anywhere, even in the middle of a keyword –Other languages like PL/I or Early Fortran allow keywords to be used as identifiers This makes them difficult to scan and parse It also reduces readability 10/18/2015IT 32722

Historical Note #2 Some languages have a fixed-format lexical structure -- column positions are significant –One statement per line (i.e. per card) –First few columns for statement label –Etc. Early dialects of Fortran, Cobol, and Basic Almost all modern languages are free-format: column positions are ignored 10/18/2015IT 32723

Other Grammar Forms BNF variations EBNF variations Syntax diagrams 10/18/2015IT 32724

BNF Variations Some use  or = instead of ::= Some leave out the angle brackets and use a distinct typeface for tokens Some allow single quotes around tokens, for example to distinguish ‘ | ’ as a token from | as a meta-symbol 10/18/2015IT Sir, please Step away from the ASR-33 Interesting operator!! Or not!

EBNF Variations Additional syntax to simplify some grammar chores: –{x} to mean zero or more repetitions of x –[x] to mean x is optional (i.e. x | ) –() for grouping –| anywhere to mean a choice among alternatives –Quotes around tokens, if necessary, to distinguish from meta-symbols 10/18/2015IT 32726

EBNF Examples Anything that extends BNF this way is called an Extended BNF: EBNF There are many variations 10/18/2015IT ::= { ;} ::= if then [else ] ::= { ( | ) ;}

Syntax Diagrams Syntax diagrams (“railroad diagrams”) 10/18/2015IT ifthenelse exprstmt if-stmt ::= if then else

Bypasses 10/18/2015IT ifthenelse exprstmt if-stmt ::= if then [else ]

Branching 10/18/2015IT ::= + | * | ( ) | a | b | c

Loops 10/18/2015IT ::= {+ }

Syntax Diagrams, Pro and Con Easier for human to read (follow) Difficult to perceive the phrase structures (syntax tree)? Harder for machine to read (for automatic parser-generators) 10/18/2015IT 32732

Conclusion We use grammars to define programming language syntax, both lexical structure and phrase structure Connection between theory and practice –Two grammars, two compiler passes –Parser-generators can produce code for those two passes automatically from grammars (compiler tools) 10/18/2015IT 32733