PROGRAMMING LANGUAGES

Slides:



Advertisements
Similar presentations
1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Advertisements

Introduction The compilation approach uses a program called a compiler, which translates programs written in a high-level programming language into machine.
Context-Free Grammars Lecture 7
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
CSC 338: Compiler design and implementation
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
ISBN Chapter 3 Describing Syntax and Semantics.
CPS 506 Comparative Programming Languages Syntax Specification.
D Goforth COSC Translating High Level Languages.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
D Goforth COSC Translating High Level Languages Note error in assignment 1: #4 - refer to Example grammar 3.4, p. 126.
1 Parsers and Grammar. 2 Categories of Grammar Rules  Declarations or definitions. AttributeDeclaration ::= [ final ] [ static ] [ access ] datatype.
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 4.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Department of Software & Media Technology
CS 3304 Comparative Languages
Chapter 3 – Describing Syntax
lec02-parserCFG May 8, 2018 Syntax Analyzer
Parsing — Part II (Top-down parsing, left-recursion removal)
CS 3304 Comparative Languages
A Simple Syntax-Directed Translator
Introduction to Parsing
CS 326 Programming Languages, Concepts and Implementation
Programming Languages Translator
CS510 Compiler Lecture 4.
Lexical and Syntax Analysis
Chapter 2 :: Programming Language Syntax
Parsing and Parser Parsing methods: top-down & bottom-up
Introduction to Parsing (adapted from CS 164 at Berkeley)
Short introduction to compilers
Chapter 3 – Describing Syntax
Parsing — Part II (Top-down parsing, left-recursion removal)
Compiler Lecture 1 CS510.
Parsing Techniques.
CSE 3302 Programming Languages
Lexical and Syntax Analysis
Introduction CI612 Compiler Design CI612 Compiler Design.
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Review: Compiler Phases:
R.Rajkumar Asst.Professor CSE
CS 3304 Comparative Languages
Lecture 4: Lexical Analysis & Chomsky Hierarchy
CS 3304 Comparative Languages
Programming Languages 2nd edition Tucker and Noonan
Parsing — Part II (Top-down parsing, left-recursion removal)
Chapter 2 :: Programming Language Syntax
Chapter 3 Describing Syntax and Semantics.
BNF 9-Apr-19.
Chapter 2 :: Programming Language Syntax
High-Level Programming Language
Chapter 10: Compilers and Language Translation
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
lec02-parserCFG May 27, 2019 Syntax Analyzer
Faculty of Computer Science and Information System
Presentation transcript:

PROGRAMMING LANGUAGES CS 222 LECTURE 06 PROGRAMMING LANGUAGES

Syntax and Semantics The syntax of a language defines the valid symbols and grammar. Syntax defines the structure of a program, i.e., the form that each program unit and each statement must use. The semantics defines the meaning of the grammar elements. Lexical structure is the form of lowest level syntactic units (words or tokens) of a grammar.

Syntax and Semantics Compared Syntax: in Java, an assignment statement is: identifier = expression { operator expression } ; Semantics: an assignment statement must use compatible types, e.g. int n1, n2; n1 = 20*1024; // OK, int_var = int_expression n2 = 3.50; // illegal, incompatible types Lexical elements (Lexemes): "n2" "=" "3.50" ";"

Parts of a Compiler / Interpreter: How are they used? Program Source Code Token stream Parse tree Intermediate code Tokenizer (Lexical Analysis) Parser (Syntax Analysis) Semantic Analysis Optimization and Code Generation Object code Parts of a Compiler / Interpreter:

Scanning and Parsing source file Scanner Parser input stream parse tree sum = x1 + x2; sum = x1 + x2 ; x1 x2 tokens

Scanners Recognize regular expressions Implemented as finite automata (finite state machines) Typically contain a loop that cycles through characters, building tokens and associated values by repeated operations scanner may be integrated as a function in the parser. Parser calls the Scanner to get the next token.

Scanners

Lexical Structure Lexemes are the smallest lexical unit of a language, grouped according to syntactic usage. Some types of lexemes in computer languages are: identifiers: x, println, _INIT, ArrayList numeric constants: 0, 10000, 2.98E+6 operators: =, +, -, ++, +=, *, / separators: [ ] ; : . , ( ) string literals: "hello there"

Lexical Structure A token is a structure representating a lexeme that explicitly indicates its categorization for the purpose of parsing. Lexemes are recognized by the first phase of a translator -- the scanner -- that deals directly with the input. The scanner separates the input into tokens. Scanners are also called lexers.

Types of lexemes Common Lexemes (classes of tokens) identifiers: x, println, _INIT, ArrayList numeric constants: 0, 10000, 2.98E+6 assignment operators: =, +=, -=, *=, /=, %= arithmetic operators: *, /, +, -, % boolean operators: &&, ||, ^, ! separators: [ ] ; : . , ( ) string literals: "hello there“ Reserved words: may be defined as a class, or simply treat as identifiers at lexical level

Tokens = assignment operator ( expression delimiter sum identifier Tokens are the strings of syntactic units. Example: what are the tokens in this statement? result = (sum - average)/count; Lexeme Tokens: result identifier = assignment operator ( expression delimiter sum identifier - arithmetic operator average identifier ) expression delimiter / arithmetic operator count identifier ; semi-colon (statement delimiter)

Here is an FA that recognizes a subset of tokens in the Pascal language:

when scanning “ temp := temp + 1 ” The first token should be temp. From start state, then go to state q1 and loop in state q1 until get “ : ” . It will stop and return the first token “temp” then start to get the next token. Try scanning “ x := (y+10) * z1; ”

Parsing Algorithms Broadly divided into LL and LR. LL algorithms match input directly to left-side symbols, then choose a right-side production that matches the tokens. This is top-down parsing LR algorithms try to match tokens to the right- side productions, then replace groups of tokens with the left-side nonterminal. They continue until the entire input has been "reduced" to the start symbol

Parsing Algorithms Look ahead: algorithms must look at next token(s) to decide between alternate productions for current tokens LL(1) means LL with 1 token look-ahead LL algorithms are simpler and easier to visualize. LR algorithms are more powerful: can parse some grammars that LL cannot, such as left recursion.

Top-down Parsing Example (LL) Grammar rule : Input String : x – 2 * y Tokens : id – number * id Rule Number 1 2 3 4 5 6 7 8 9 10

Top-down Parsing Example (LL) Grammar rule : Input String : x – 2 * y Tokens : id – number * id Rule Number 1 2 3 4 5 6 7 8 9 10

Elimination of Left Recursion

Elimination of Left Recursion Here is the grammar again: S ::= A | B A ::= ABc | AAdd | a | aa B ::= Bee | b An equivalent right-recursive grammar: A ::= aA′ | aaA′ A′ ::= BcA′ | AddA′ |  B ::= bB′ B′ ::= eeB′ | 

Eliminating Left Recursion

LL Parsing Example Let try input String : x – 2 * y Tokens : id – number * id

Rule Sentential Form Input - Goal Expr Term Expr Factor Term Expr <id,x> Term Expr <id,x>  Expr <id,x> +Term Expr <id,x> - Term Expr <id,x> - Factor Term Expr <id,x> - <number,2> Term Expr <id,x> - <number,2> * Factor Term Expr <id,x> - <number,2> * <id,y> Term Expr <id,x> - <number,2> * <id,y>  Expr <id,x> - <number,2> * <id,y>  <id,x> - <number,2> * <id,y> x – 2 * y x – 2 * y x – 2 * y x – 2  * y x – 2 *  y x – 2 * y 