By Neng-Fa Zhou Programming language syntax 4 Three aspects of languages –Syntax How are sentences formed? –Semantics What does a sentence mean? –Pragmatics.

Slides:



Advertisements
Similar presentations
Chapter 5: Languages and Grammar 1 Compiler Designs and Constructions ( Page ) Chapter 5: Languages and Grammar Objectives: Definition of Languages.
Advertisements

Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Grammars, Languages and Parse Trees. Language Let V be an alphabet or vocabulary V* is set of all strings over V A language L is a subset of V*, i.e.,
Winter 2007SEG2101 Chapter 81 Chapter 8 Lexical Analysis.
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax Lecture 2 - Syntax, Spring CSE3302 Programming Languages, UT-Arlington ©Chengkai.
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Context-Free Grammars Lecture 7
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Compiler Constreuction 1 Chapter 4 Syntax Analysis Topics to cover: Context-Free Grammars: Concepts and Notation Writing and rewriting a grammar Syntax.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
Chapter 3: Formal Translation Models
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
COP4020 Programming Languages
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
Prof. Fateman CS 164 Lecture 71 More Finite Automata/ Lexical Analysis /Introduction to Parsing Lecture 7.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Compiler Phases: Source program Lexical analyzer Syntax analyzer Semantic analyzer Machine-independent code improvement Target code generation Machine-specific.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Context-Free Grammars
Grammars CPSC 5135.
PART I: overview material
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
Other Issues - § 3.9 – Not Discussed More advanced algorithm construction – regular expression to DFA directly.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Bernd Fischer RW713: Compiler and Software Language Engineering.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
By Neng-Fa Zhou Lexical Analysis 4 Why separate lexical and syntax analyses? –simpler design –efficiency –portability.
Introduction to Parsing
CPS 506 Comparative Programming Languages Syntax Specification.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
LESSON 04.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Overview of Previous Lesson(s) Over View  In our compiler model, the parser obtains a string of tokens from the lexical analyzer & verifies that the.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax Analyzer (Parser)
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
LECTURE 7 Lex and Intro to Parsing. LEX Last lecture, we learned a little bit about how we can take our regular expressions (which specify our valid tokens)
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 3.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
Chapter 2: A Simple One Pass Compiler
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Syntax(1). 2 Syntax  The syntax of a programming language is a precise description of all its grammatically correct programs.  Levels of syntax Lexical.
Last Chapter Review Source code characters combination lexemes tokens pattern Non-Formalization Description Formalization Description Regular Expression.
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Chapter 4 - Parsing CSCE 343.
Chapter 3 Context-Free Grammar and Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Lexical Analysis Why separate lexical and syntax analyses?
Syntax Analysis source program lexical analyzer tokens syntax analyzer
Review: Compiler Phases:
Chapter 2: A Simple One Pass Compiler
CSC 4181Compiler Construction Context-Free Grammars
Other Issues - § 3.9 – Not Discussed
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CSC 4181 Compiler Construction Context-Free Grammars
COMPILER CONSTRUCTION
Presentation transcript:

by Neng-Fa Zhou Programming language syntax 4 Three aspects of languages –Syntax How are sentences formed? –Semantics What does a sentence mean? –Pragmatics How to use the language? 4 Only syntax can be described formally –Regular expressions and context-free grammars

by Neng-Fa Zhou Regular expressions (RE)  The empty string  is a RE  Every symbol in  (alphabet) is a RE 4 Let r and s be REs. –r | s: or –rs: concatenation –(r) * : zero or more instances –(r) + : one or more instances –(r)?: zero or one instance

by Neng-Fa Zhou Precedence of operators high low r* r + r? rs r|s all left associative 4 Examples  = {a,b} 1. a|b 2. (a|b)(a|b) 3. a* 4. (a|b)* 5. a| a*b

by Neng-Fa Zhou Algebraic Properties of RE

by Neng-Fa Zhou d 1 r 1 d 2 r 2 d n r n.... d i is a RE over  {d 1,d 2,...,d i-1 } Regular Definitions not recursive

by Neng-Fa Zhou Examples 4 Identifiers 4 Decimal integers in Java 4 Hexadecimal integers letter  A | B |... | Z | a | b |... | z digit  0 | 1 |... | 9 id  letter ( letter | digit )* DecimalNumeral  0 | nonZeroDigit digit* HexaNumeral  (0x | 0X) hexadigit+

by Neng-Fa Zhou Lex 4 A tool for automatically generating lexical analyzers

by Neng-Fa Zhou Lex Specifications declarations % translation rules % auxiliary procedures p 1 {action 1 } p 2 {action 2 }... p n {action n }

by Neng-Fa Zhou Lex Regular Expressions

Example-1 by Neng-Fa Zhou %{ int num_lines = 0, num_chars = 0; %} % \n ++num_lines; ++num_chars;. ++num_chars; % main() { yylex(); printf( "# of lines = %d, # of chars = %d\n", num_lines, num_chars ); } yywrap(){return 0;}

by Neng-Fa Zhou Example-2 D [0-9] INT {D}{D}* % {INT}("."{INT}((e|E)("+"|-)?{INT})?)? {printf("valid %s\n",yytext);}. {printf("unrecognized %s\n",yytext);} % int main(int argc, char *argv[]){ ++argv, --argc; if (argc>0) yyin = fopen(argv[0],"r"); else yyin = stdin; yylex(); } yywrap(){return 0;}

java.util.regex by Neng-Fa Zhou import java.util.regex.*; class Number { public static void main(String[] args){ String regExNum = "\\d+(\\.\\d+((e|E)(\\+|-)?\\d+)?)?"; if (Pattern.matches(regExNum,args[0])) System.out.println("valid"); else System.out.println("invalid"); }

String Pattern Matching in Perl by Neng-Fa Zhou print "Input a string :"; $_ = ; chomp($_); if (/^[0-9]+(\.[0-9]+((e|E)(\+|-)?[0-9]+)?)?$/){ print "valid\n"; } else { print "invalid\n"; }

by Neng-Fa Zhou Context-free Grammars –  is a finite set of terminals – N is a finite set of non-terminals – P is a finite subset of production rules – S is the start symbol G=( ,N,P,S)

by Neng-Fa Zhou E  T | E + T | E - T T  F | T * F |T / F F  id | (E) CFG: Examples 4 Arithmetic expressions 4 Statements IfStatement  if E then Statement else Statement

by Neng-Fa Zhou CFG vs. Regular Expressions 4 CFG is more expressive than RE –Every language that can be described by regular expressions can also be described by a CFG 4 Example languages that are CFG but not RE –if-then-else statement, {a n b n | n>=1} 4 Non-CFG –L1={wcw | w is in (a|b)*} –L2={a n b m c n d m | n>=1 and m>=1}

by Neng-Fa Zhou Derivations  if      andthen * ** S  *  is a sentential form  is a sentence if it contains only terminal symbols

by Neng-Fa Zhou Derivations 4 leftmost derivation 4 Rightmost derivation  if  is a string of terminals  if  is a string of terminals

by Neng-Fa Zhou Parse Trees 4 A parse tree is any tree in which –The root is labeled with S –Each leaf is labeled with a token a or  –Each interior node is labeled by a nonterminal –If an interior node is labeled A and has children labeled X1,.. Xn, then A  X1...Xn is a production.

by Neng-Fa Zhou Parse Trees and Derivations E  E + E | E * E | E - E | - E | ( E ) | id

by Neng-Fa Zhou YACC %token DIGIT % lines: lines expr '\n'{printf("%d\n",$2);} | lines '\n' | ; expr: expr '+' term{$$ = $1 + $3;} | expr '-' term{$$ = $1 - $3;} | term ; term: term '*' factor {$$ = $1 * $3;} | term '/' factor {$$ = $1 / $3;} | factor ; factor : '(' expr ')'{$$ = $2;} | DIGIT ; %

DCG in Prolog Strings with an equal number of 0’s and 1’s 4 DCG 4 Prolog clauses by Neng-Fa Zhou :-table e/2. e --> []. e --> [0],e,[1]. e --> [1],e,[0]. e --> e,e. :-table e/2. e(A, A). e(A, B) :- 'C'(A, 0, C), e(C, D), 'C'(D, 1, B). e(A, B) :- 'C'(A, 1, C), e(C, D), 'C'(D, 0, B). e(A, B) :- e(A, C), e(C, B).