Compiler Construction. Lecture 10

Similar documents
Variables. Data Types.

Compiler Construction(CS606)- Lecture Handouts

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

Course Organization. The grade distribution will be. Theory Homeworks 10% Exams 50% Practice Project 40%

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Basics of C++ // my first program in C++ Hello World! #include <iostream> using namespace std; int main () { cout << "Hello World!

Introduction to C++ Systems Programming

Basic Types, Variables, Literals, Constants

Introduction to Programming

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Tokens, Expressions and Control Structures

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Programming with C++ Language

MP 3 A Lexer for MiniJava

Programming in C++ 4. The lexical basis of C++

Chapter 15 - C++ As A "Better C"

IS 0020 Program Design and Software Tools

Compiler Construction. Lecture 9

Chapter 2 - Control Structures

APPENDIX A : KEYWORDS... 2 APPENDIX B : OPERATORS... 3 APPENDIX C : OPERATOR PRECEDENCE... 4 APPENDIX D : ESCAPE SEQUENCES... 5

Assignment Operations

EEE145 Computer Programming

Programming. C++ Basics

EP241 Computer Programming

Chapter 2 - Control Structures

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Manual. Subject Code: CS593. Computer Science and Engineering

CMSC445 Compiler design Blaheta. Project 2: Lexer. Due: 15 February 2012

LEXICAL 2 CONVENTIONS

6.096 Introduction to C++ January (IAP) 2009

ANSI C Changes. Jonathan Hoyle Eastman Kodak 10/5/00

C++ INDEX. Introduction: Instructions for use. Basics of C++: Structure of a program Variables. Data Types. Constants Operators Basic Input/Output

CS3157: Advanced Programming. Outline

Chapter 2 - Control Structures

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Appendix. Compatibility. B.1 Introduction. You go ahead and follow your customs, and I ll follow mine. C. Napier

W3101: Programming Languages C++ Ramana Isukapalli

cs3157: c++ lecture #2 (mon-11-apr-2005) chronology of some programming languages... C++ vs Java identifiers.

1

Lecture 02 C FUNDAMENTALS

OBJECT ORIENTED PROGRAMMING USING C++

STUDY NOTES UNIT 1 - INTRODUCTION TO OBJECT ORIENTED PROGRAMMING

Chapter 2

EP578 Computing for Physicists

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Migrating from C to C++

Syntax Analysis Part VIII

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

LECTURE 11. Semantic Analysis and Yacc

UEE1302 (1102) F10: Introduction to Computers and Programming

std::cout << "Size of long = " << sizeof(long) << " bytes\n\n"; std::cout << "Size of char = " << sizeof(char) << " bytes\n";

Presented By : Gaurav Juneja

Lexical Analysis. Introduction

Lexical analysis. Syntactical analysis. Semantical analysis. Intermediate code generation. Optimization. Code generation. Target specific optimization

Lexical Analysis. Implementing Scanners & LEX: A Lexical Analyzer Tool

UNIT-2 Introduction to C++

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

Preface to the Second Edition Preface to the First Edition Brief Contents Introduction to C++ p. 1 A Review of Structures p.

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Lexical and Syntax Analysis

IBM i Version 7.2. Programming IBM Rational Development Studio for i ILE C/C++ Language Reference IBM SC

C: How to Program. Week /Mar/05

This tutorial adopts a simple and practical approach to describe the concepts of C++.

Parsing and Pattern Recognition

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CSCI 123 Introduction to Programming Concepts in C++

Syntax and Variables

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Chapter 2 - Introduction to C Programming

TIOBE - C Coding Standard Version head

Dr. Md. Humayun Kabir CSE Department, BUET

12. A bit of C++ Oscar Nierstrasz. Friday, October 21, 11

C++ Casts and Run-Time Type Identification

Compilers. Lecture 2 Overview. (original slides by Sam

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

The component base of C language. Nguyễn Dũng Faculty of IT Hue College of Science

XSEDE Scholars Program Introduction to C Programming. John Lockman III June 7 th, 2012

The Foundation of C++: The C Subset An Overview of C p. 3 The Origins and History of C p. 4 C Is a Middle-Level Language p. 5 C Is a Structured

Preface... (vii) CHAPTER 1 INTRODUCTION TO COMPUTERS

Syntax Analysis Part IV

COMP322 - Introduction to C++

Welcome to Teach Yourself Acknowledgments Fundamental C++ Programming p. 2 An Introduction to C++ p. 4 A Brief History of C++ p.

Programming Assignment II

Programming Fundamentals (CS 302 ) Dr. Ihsan Ullah. Lecturer Department of Computer Science & IT University of Balochistan

TDDD55- Compilers and Interpreters Lesson 2

In this session we will cover the following sub-topics: 1.Identifiers 2.Variables 3.Keywords 4.Statements 5.Comments 6.Whitespaces 7.Syntax 8.

PRACTICAL CLASS: Flex & Bison

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Programming Assignment I Due Thursday, October 7, 2010 at 11:59pm

Fundamentals of Programming

Undergraduate Compilers in a Day

Other Topics. Objectives. In this chapter you ll:

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Axivion Bauhaus Suite Technical Factsheet AUTOSAR

Syntax Intro and Overview. Syntax

Introduction to C# Applications

Transcription:

Compiler Construction Lecture 10

Using Generated Scanner void main() { FlexLexer lex; int tc = lex.yylex(); while(tc!= 0) cout << tc <<, <<lex.yytext() << endl; tc = lex.yylex(); } 2

Input Tokenized dos>.\lex < main.cpp 259,void 258,main 283,( 284,) 285,{ 258,FlexLexer 258,lex 290,; 260,int 3

Input Tokenized 258,tc 266,= 258,lex 291,. 258,yylex 283,( 284,) 290,; 263,while 4

Input Tokenized 283,( 258,tc 276,!= 257,0 284,) 258,cout 279,<< 258,tc 5

Input Tokenized 279,<< 292,"," 279,<< 258,lex 291,. 258,YYText 283,( 284,) 279,<< 6

Input Tokenized 258,endl 290,; 258,tc 266,= 258,lex 291,. 258,yylex 283,( 284,) 290,; 286,} 7

Flex Input for C++ /* * ISO C++ lexical analyzer. * Based on the ISO C++ draft standard of December '96. */ %{ #include <ctype.h> #include <stdio.h> #include tokdefs.h" int lineno; static int yywrap(void); static void skip_until_eol(void); static void skip_comment(void); static int check_identifier(const char *); %} 8

intsuffix ([uu][ll]?) ([ll][uu]?) fracconst ([0-9]*\.[0-9]+) ([0-9]+\.) exppart [ee][-+]?[0-9]+ floatsuffix [ffll] chartext ([^']) (\\.) stringtext ([^"]) (\\.) %% 9

%% "\n" { ++lineno; } [\t\f\v\r ]+ { /* Ignore whitespace. */ } "/*" { skip_comment(); } "//" { skip_until_eol(); } "{" { return '{'; } "<%" { return '{'; } "}" { return '}'; } "%>" { return '}'; } "[" { return '['; } "<:" { return '['; } 10

"]" { return ']'; } ":>" { return ']'; } "(" { return '('; } ")" { return ')'; } ";" { return ';'; } ":" { return ':'; } "..." { return ELLIPSIS; } "?" { return '?'; } "::" { return COLONCOLON; } "." { return '.'; } ".*" { return DOTSTAR; } "+" { return '+'; } "-" { return '-'; } "*" { return '*'; } "/" { return '/'; } "%" { return '%'; } "^" { return '^'; } "xor" { return '^'; } "&" { return '&'; } "bitand" { return '&'; } 11

" " { return ' '; } "bitor" { return ' '; } "~" { return '~'; } "compl" { return '~'; } "!" { return '!'; } "not" { return '!'; } "=" { return '='; } "<" { return '<'; } ">" { return '>'; } "+=" { return ADDEQ; } "-=" { return SUBEQ; } "*=" { return MULEQ; } "/=" { return DIVEQ; } "%=" { return MODEQ; } "^=" { return XOREQ; } "xor_eq" { return XOREQ; } "&=" { return ANDEQ; } "and_eq" { return ANDEQ; } " =" { return OREQ; } "or_eq" { return OREQ; } 12

"<<" { return SL; } ">>" { return SR; } "<<=" { return SLEQ; } ">>=" { return SREQ; } "==" { return EQ; } "!=" { return NOTEQ; } "not_eq" { return NOTEQ; } "<=" { return LTEQ; } ">=" { return GTEQ; } "&&" { return ANDAND; } "and" { return ANDAND; } " " { return OROR; } "or" { return OROR; } "++" { return PLUSPLUS; } "--" { return MINUSMINUS; } "," { return ','; } "->*" { return ARROWSTAR; } "->" { return ARROW; } 13

"asm" { return ASM; } "auto" { return AUTO; } "bool" { return BOOL; } "break" { return BREAK; } "case" { return CASE; } "catch" { return CATCH; } "char" { return CHAR; } "class" { return CLASS; } "const" { return CONST; } "const_cast" { return CONST_CAST; } "continue" { return CONTINUE; } "default" { return DEFAULT; } "delete" { return DELETE; } "do" { return DO; } "double" { return DOUBLE; } "dynamic_cast" { return DYNAMIC_CAST; } "else" { return ELSE; } "enum" { return ENUM; } "explicit" { return EXPLICIT; } "export" { return EXPORT; } 14

"extern" { return EXTERN; } "false" { return FALSE; } "float" { return FLOAT; } "for" { return FOR; } "friend" { return FRIEND; } "goto" { return GOTO; } "if" { return IF; } "inline" { return INLINE; } "int" { return INT; } "long" { return LONG; } "mutable" { return MUTABLE; } "namespace" { return NAMESPACE; } "new" { return NEW; } "operator" { return OPERATOR; } "private" { return PRIVATE; } "protected" { return PROTECTED; } "public" { return PUBLIC; } "register" { return REGISTER; } "reinterpret_cast" { return REINTERPRET_CAST; } "return" { return RETURN; } 15

"short" { return SHORT; } "signed" { return SIGNED; } "sizeof" { return SIZEOF; } "static" { return STATIC; } "static_cast" { return STATIC_CAST; } "struct" { return STRUCT; } "switch" { return SWITCH; } "template" { return TEMPLATE; } "this" { return THIS; } "throw" { return THROW; } "true" { return TRUE; } "try" { return TRY; } "typedef" { return TYPEDEF; } "typeid" { return TYPEID; } "typename" { return TYPENAME; } "union" { return UNION; } "unsigned" { return UNSIGNED; } "using" { return USING; } "virtual" { return VIRTUAL; } "void" { return VOID; } 16

"volatile" { return VOLATILE; } "wchar_t" { return WCHAR_T; } "while" { return WHILE; } [a-za-z_][a-za-z_0-9]* { return check_identifier(yytext); } "0"[xX][0-9a-fA-F]+{intsuffix}? { return INTEGER; } "0"[0-7]+{intsuffix}? { return INTEGER; } [0-9]+{intsuffix}? { return INTEGER; } 17

{fracconst}{exppart}?{floatsuffix}? { return FLOATING; } [0-9]+{exppart}{floatsuffix}? { return FLOATING; } "'"{chartext}*"'" { return CHARACTER; } "L'"{chartext}*"'" { return CHARACTER; } "\""{stringtext}*"\"" { return STRING; } "L\""{stringtext}*"\"" { return STRING; } 18

. { fprintf(stderr, "%d: unexpected character `%c'\n", lineno, yytext[0]); } %% static int yywrap(void) { return 1; } 19

static void skip_comment(void) { int c1, c2; c1 = input(); c2 = input(); } while(c2!= EOF &&!(c1 == '*' && c2 == '/')) { if (c1 == '\n') ++lineno; c1 = c2; c2 = input(); } 20

static void skip_until_eol(void) { int c; } while ((c = input())!= EOF && c!= '\n') ; ++lineno; 21

static int check_identifier(const char *s) { /* * This function should check if `s' is a * typedef name or a class * name, or a enum name,... etc. or * an identifier. */ switch (s[0]) { case 'D': return TYPEDEF_NAME; case 'N': return NAMESPACE_NAME; case 'C': return CLASS_NAME; case 'E': return ENUM_NAME; case 'T': return TEMPLATE_NAME; } return IDENTIFIER; } 22

Parsing

Front-End: Parser source code scanner tokens parser IR errors Checks the stream of words and their parts of speech for grammatical correctness 24

Front-End: Parser source code scanner tokens parser IR errors Determines if the input is syntactically well formed 25

Front-End: Parser source code scanner tokens parser IR errors Guides context-sensitive ( semantic ) analysis (type checking) 26

Front-End: Parser source code scanner tokens parser IR errors Builds IR for source program 27

Syntactic Analysis Natural language analogy: consider the sentence He wrote the program 28

Syntactic Analysis He wrote the program noun verb article noun 29

Syntactic Analysis He wrote the program noun verb article noun subject predicate object 30

Syntactic Analysis Natural language analogy He wrote the program noun verb article noun subject predicate object sentence 31

Syntactic Analysis Programming language if ( b <= 0 ) a = b bool expr assignment if-statement 32

Syntactic Analysis syntax errors int* foo(int i, int j)) { for(k=0; i j; ) fi( i > j ) return j; } 33