What does it mean? Notes from Robert Sebesta Programming Languages

Slides:



Advertisements
Similar presentations
ICE1341 Programming Languages Spring 2005 Lecture #4 Lecture #4 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Advertisements

ISBN Chapter 3 Describing Syntax and Semantics.
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.
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.
CS 330 Programming Languages 09 / 11 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 3 Describing Syntax and Semantics.
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.
Describing Syntax and Semantics
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
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.
C H A P T E R TWO Syntax and Semantic.
ISBN Chapter 3 Describing Syntax and Semantics.
Course: ICS313 Fundamentals of Programming Languages. Instructor: Abdul Wahid Wali Lecturer, College of Computer Science and Engineering.
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.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
CPS 506 Comparative Programming Languages Syntax Specification.
D Goforth COSC Translating High Level Languages.
Chapter 3 Describing Syntax and Semantics. Chapter 3: Describing Syntax and Semantics - Introduction - The General Problem of Describing Syntax - Formal.
C HAPTER 3 Describing Syntax and Semantics. T OPICS Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
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
ISBN Chapter 3 Describing Syntax and Semantics.
Syntax and Grammars.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
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.
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
Describing Syntax and Semantics
CS510 Compiler Lecture 4.
Chapter 3 – Describing Syntax
Concepts of Programming Languages
Syntax (1).
Compiler Construction
Syntax versus Semantics
CS 363 Comparative Programming Languages
CSE 3302 Programming Languages
Compiler Design 4. Language Grammars
Describing Syntax and Semantics
Programming Language Syntax 2
Chapter 2: A Simple One Pass Compiler
R.Rajkumar Asst.Professor CSE
CS 3304 Comparative Languages
CS 3304 Comparative Languages
September 13th Grammars.
Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
Describing Syntax and Semantics
Programming Languages 2nd edition Tucker and Noonan
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Presentation transcript:

What does it mean? Notes from Robert Sebesta Programming Languages Syntax and Semantics What does it mean? Notes from Robert Sebesta Programming Languages

Syntax Rules for the language In English some syntax rules are: Sentence starts with capital letter Sentence ends with ‘.’, ‘?’, ‘!’ Names are capitalized Spaces between words

Syntax In programming, is the form of expressions, statements, and program units if(a==b) { … } else { }

Semantics Is meaning. In the preceding statement the meaning is that the computer must evaluate a conditional and make a choice based on it.

Lexemes A language is just a set of strings of some alphabet Lexemes – smallest units usable in a language (i.e. letters, numbers, symbols) const, for, while, if, a, b, int, 1 Programs are more a string of lexemes than a string of characters

Tokens Lexemes are broken into categories or groups called tokens A token might be Identifiers (a variable or function name for instance) Literals (numbers for instance) Operators (equal sign, addition sign, etc.)

Tokens (example) index = 2 * count + 17; Lexemes Tokens index identifier = Equal_sign 2 Int_literal * Mult_op count + Plus_op 17 ; semicolon

Formally Defining Languages: Language Recognizers If we have a language L that uses an alphabet Σ And we have a mechanism R that could read strings of characters from Σ R could accept a string only if it is in L Example is the syntax analysis part of a compiler. Determines if given programs are syntactically correct.

Formally Defining Languages: Language Generators A device that can be used to generate the sentences of a language. Imagine a button that produces a sentence from the language each time it is pushed. May be more useful to a programmer; can produce sentences we can compare our code to to determine correct syntax without having to feed it to a compiler and hope for the best

Context-Free Grammars Developed by linguist Noam Chomsky Is a set of rules whereby possible strings are formed by replacements All possible strings in the language can be produced through replacements

Backus-Naur Form (BNF) Developed at the same time as Chomsky’s ideas, but in completely different research efforts Was created for the description of ALGOL 60 Nearly identical to Chomsky’s work

How do they work? Both use metalanguage to describe an abstraction <assign> -> <var> = <expression> Text on left-hand side is the abstraction being defined Text on right-hand side is the definition Together is called a rule or a production One (of MANY) example sentences this describes is: x = y + z

Terminology Nonterminal symbols – the abstractions in the BNF description Terminal symbols – lexemes and tokens

Example <if_stmt> -> if ( <logic_expr> ) <stmt> | if ( <logic_expr> ) <stmt> else <stmt>

Rules can be recursive… <ident_list> -> <identifier> | <identifier>, <ident_list> This one says that a list is either a single token or an identifier followed by a comma and another <ident_list>

Begins with a start symbol We may call it whatever we wish, but useful names might be <program> <statements> <instruction_list> Etc…

Example (from book) A = B * (A + C)

Leftmost derivation <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 )

In-class (no more than 2 or 3 mins…) A = ( B + A ) * A + B

Because it isn’t valid! (no rule for <expr> * <expr>

This one is though! C = A * ( A + B * C )

Parse Trees Grammars describe hierarchy well. Therefore we can view productions visually.

Parse Trees Every internal node is a non-terminal symbol Every leaf is a terminal

Ambiguity When there are two or more parse trees that are valid. NOT GOOD!

For instance

A = B + C * A

Semantics

Order of Operations * and + have different meanings. The previous grammar didn’t deal with that. * should have a higher precedence and therefore be lower in the parse tree We must rewrite the grammar. We can do this by using additional non- terminals and new rules

Unambiguous Grammar

Sentence Generated

Parse Tree

Associativity Two operators of the same level of precedence * and / Or two of the same operators on one line + and + Not always an issue but can be. 10 / 5 / 5 = 10 / (5 / 5) or (10 / 5 ) / 5 ? Must have a rule in place. i.e. left to right associativity

Lexical Analysis and Syntax Analysis

Lexical Analyzer ”Front-end” of the syntax analyzer Is a pattern matcher (regexes!) Collects characters into ”lexemes” We group lexemes into tokens

Lexical Analysis