Chapter 3 – Describing Syntax

Slides:



Advertisements
Similar presentations
ISBN Chapter 3 Describing Syntax and Semantics.
Advertisements

CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
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.
1 CSE305 Programming Languages Syntax What is it? How is it specified? Who uses it? Why is it needed?
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
ISBN Chapter 3 Describing Syntax and Semantics.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Chapter 2 Syntax A language that is simple to parse for the compiler is also simple to parse for the human programmer. N. Wirth.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Grammars CPSC 5135.
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
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.
ISBN Chapter 3 Describing Syntax and Semantics.
Syntax and Grammars.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
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.
1 CS Programming Languages Class 04 September 5, 2000.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Describing Syntax and Semantics Chapter 3: Describing Syntax and Semantics Lectures # 6.
CS 3304 Comparative Languages
BBM Programming Languages
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
PROGRAMMING LANGUAGES
Describing Syntax and Semantics
Concepts of Programming Languages
Describing Syntax and Semantics
CS510 Compiler Lecture 4.
Programming Languages
Concepts of Programming Languages
Syntax (1).
What does it mean? Notes from Robert Sebesta Programming Languages
Syntax versus Semantics
Compiler Construction (CS-636)
CS 363 Comparative Programming Languages
Describing Syntax and Semantics
CSE 3302 Programming Languages
Describing Syntax and Semantics
Describing Syntax and Semantics
3 Syntax.
R.Rajkumar Asst.Professor CSE
Describing Syntax and Semantics
CS 3304 Comparative Languages
Chapter 3: Describing Syntax and Semantics Sangho Ha
September 13th Grammars.
Describing Syntax and Semantics
Describing Syntax and Semantics
Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
- Who must use language definitions?
Describing Syntax and Semantics
Programming Languages 2nd edition Tucker and Noonan
COMPILER CONSTRUCTION
Presentation transcript:

Chapter 3 – Describing Syntax CSCE 343

Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics: the meaning of the expressions, statements, and program units. Syntax + semantics = language definition Used by: Other language designers Implementers Programmers

Terminology Sentence: string of characters over some alphabet Language: set of sentences Lexeme: lowest level syntactic unit (e.g. +, (, {, while, etc.) Token: a category of lexemes (e.g. identifier, open brace, int literal, etc.)

Chomsky’s Classes of Grammars Type-0: Unrestricted Type-1: Context Sensitive Grammars Type-2: Context Free Grammars Type-3: Regular Grammars Type 2,3 most useful for programming languages

Formal Methods For Describing Syntax Backus-Naur Form (BNF) and Context-Free Grammars Most widely known method for describing programming language syntax Extended BNF Improves readability and writability of BNF Grammars and Recognizers

Backus-Naur Form (BNF) Invented by John Backus to describe Algol 58 BNF is equivalent to context-free grammars BNF: a metalanguage (used to describe other languages) Abstractions (called nonterminals) represent classes of syntactic structures (like variables)

BNF Fundamentals Non-terminals: BNF abstractions Terminals: lexemes and tokens Grammar: a collection of rules Example rule: <while_stmt>  while ( <logic_expr> ) <block>

BNF Rules Rule has left-hand side (LHS) and right-hand side (RHS) LHS is a single non-terminal RHS consists of terminals and non-terminals Grammar is a set of rules Can have more than one RHS: Recursion for lists: <ident_list>  identifier | identifier, <ident_list>

Derivations For a language to be recognized, it must be derivable from the grammar. Derivation: repeated application of rules, starting with the start symbol (non-terminal) and ending with a sentence (all terminal symbols) Leftmost vs. rightmost

Example Grammar Grammar: <program>  <stmts> <stmts>  <stmt> | <stmt> ; <stmts> <stmt>  <var> = <expr> <var>  a | b | c | d <expr>  <var> + <var> | <var> - <var> Derivation: <program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <var> + <var> => a = b + <var> => a = b + c

Practice Write a BNF grammar for Java Boolean expressions.

Derivation Each step in the derivation: sentential form Sentence: sentential form that has only terminals Leftmost vs rightmost

Practice Use your grammar to show a leftmost and a rightmost derivation of the expression: a < b && c == d

Parse Trees A hierarchical representation of a derivation <program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <var> + <var> => a = b + <var> => a = b + c

Practice Use your grammar to show a parse tree of the expression: !a || c <= d

Parse Trees and Semantics Compilers generate code by traversing parse trees. Semantics are derived from “shape” of trees. Example: math expressions Operations lower in tree occur first.

Ambiguous Grammars <expr>  <expr> <op> <expr> | <id> <op>  * | + <id> a | b | c | d

Ambiguous Grammars Get rid of multiple recursion to create unambiguous grammar: <expr>  <expr> + <term> | <term> <term>  <term> / <id> | <id> <id>  a | b | c | d

Operator Associativity Associativity indicated by recursion: <expr>  <expr> + <term> (left associative) <expr>  <term> + <expr> (right associative)

Extended BNF Optional parts in brackets [] <proc_call> <ident> [ ( <expr_list> ) ] Alternatives are placed in parenthesis <term>  <term> (+|-) <const> Repetitions (0 or more) are in braces {} <ident>  letter { letter | digit }

BNF and EBNF BNF EBNF <expr>  <expr> + <term> <term>  <term> * <factor> | <term> / <factor> | <factor> EBNF <expr>  <term> { ( + | - ) <term> } <term>  <factor> { ( * | / ) <factor> }