Compiler Construction

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick The Programming Process rUse an editor to create a program file (source file). l contains the text of.
Advertisements

C++ Basics Variables, Identifiers, Assignments, Input/Output.
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 21 - Standard C++ Language Additions Outline 21.1Introduction 21.2 bool Data Type 21.3 static_cast.
Structure of a C program
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. The Fundamentals of C++ Basic programming elements and concepts.
SECTION 2 C++ Language Basics. Strategies for learning C++ Focus on concepts and programming techniques. (Don’t get lost in language features) Learn C++
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
KEAN UNIVERSITY Visual C++ Dr. K. Shahrabi. Developer studio Is a self-contain environment for creating, compiling, linking and testing windows program.
CS 192 Lecture 3 Winter 2003 December 5, 2003 Dr. Shafay Shamail.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
COMPUTER PROGRAMMING. Data Types “Hello world” program Does it do a useful work? Writing several lines of code. Compiling the program. Executing the program.
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
CS 161 Introduction to Programming and Problem Solving Chapter Hello World Program in C++ And Lexical Rules Herbert G. Mayer, PSU Status 10/18/2014.
C Tokens Identifiers Keywords Constants Operators Special symbols.
C-Language Keywords(C99)
 Programming Languages  First Generation Languages (Machine Language)  We Actually have to do a few things. First we have to find the operating code,
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
1 More C++ Basics Chapter 3 Lecture CSIS 10A. 2 Agenda Review  C++ Standard Numeric Types Arithmetic–way more than you want! Character (char) data type.
C++ Programming, Namiq Sultan1 Chapter 2 Introduction to C++ Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin Reference:
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Variables and Data Types.  Variable: Portion of memory for storing a determined value.  Could be numerical, could be character or sequence of characters.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics Lecture 5.
Compiler Construction Sohail Aslam Lecture 9. 2 DFA Minimization  The generated DFA may have a large number of states.  Hopcroft’s algorithm: minimizes.
Data Structure and c K.S.Prabhu Lecturer All Deaf Educational Technology.
Data Types, Primitive Types in C++, Variables – Declaration, Initialization, Scope Telerik Software Academy academy.telerik.com Learning and Development.
By Anand George SourceLens.org Copyright. All rights reserved. Content Owner - Meera R (meera at sourcelens.org)
C++ Basics Programming. COMP104 Lecture 5 / Slide 2 Introduction to C++ l C is a programming language developed in the 1970s with the UNIX operating system.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
2. C FUNDAMENTALS. Example: Printing a Message /* Illustrates comments, strings, and the printf function */ #include int main(void) { printf("To C, or.
Basic Types, Variables, Literals, Constants. What is in a Word? A byte is the basic addressable unit of memory in RAM Typically it is 8 bits (octet)
C++ Lesson 1.
Asst.Prof.Dr. Tayfun ÖZGÜR
Variables, Identifiers, Assignments, Input/Output
Chapter 1.2 Introduction to C++ Programming
Basics (Variables, Assignments, I/O)
Chapter 1.2 Introduction to C++ Programming
Data types Data types Basic types
Structure of programming languages
LESSON 3 IO, Variables and Operators
Computing with C# and the .NET Framework
مبانی کامپیوتر و برنامه سازی
CMSC 104, Section 4 Richard Chang
بسم الله الرحمن الرحيم.
Reserved Words.
Basics (Variables, Assignments, I/O)
null, true, and false are also reserved.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
C++ Basics.
פרטים נוספים בסילבוס של הקורס
Oрганизација програма
Introduction to Java Programming
درس برنامه‌سازي کامپيوتر
פרטים נוספים בסילבוס של הקורס
Keywords.
Govt. Polytechnic,Dhangar
Variables, Identifiers, Assignments, Input/Output
JavaScript Reserved Words
Introduction C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell.
Prof. Bhushan Trivedi Director GLS Institute of Computer Technology
Variables in C Declaring , Naming, and Using Variables.
Module 2 Variables, Assignment, and Data Types
2. Second Step for Learning C++ Programming • Data Type • Char • Float
Programming Language C Language.
Compiler Construction
Compiler Construction
Building Blocks of C Programming Language
Module 2 - Part 1 Variables, Assignment, and Data Types
Presentation transcript:

Compiler Construction Sohail Aslam Lecture 10 compiler: intro

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

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

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

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

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

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

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 *); %}

intsuffix ([uU][lL]?)|([lL][uU]?) fracconst ([0-9]*\.[0-9]+)|([0-9]+\.) exppart [eE][-+]?[0-9]+ floatsuffix [fFlL] chartext ([^'])|(\\.) stringtext ([^"])|(\\.) %%

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

"]" { 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 '&'; }

"|" { 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; }

"<<" { 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; }

"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; }

"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; }

"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; }

"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; }

{fracconst}{exppart}?{floatsuffix}? { return FLOATING; } "'"{chartext}*"'" { return CHARACTER; } "L'"{chartext}*"'" { return CHARACTER; } "\""{stringtext}*"\"" { return STRING; } "L\""{stringtext}*"\"" { return STRING; }

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

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

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

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;

Parsing

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

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

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

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

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

Syntactic Analysis He wrote the program noun verb article noun

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

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

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

Syntactic Analysis syntax errors int* foo(int i, int j)) { for(k=0; i j; ) fi( i > j ) return j; } end of lec 10 compiler: intro