Running The Scanner-Compiler Construction-Lecture Notes, Study notes of Compiler Construction

Compile is software which translates high level programming language to computer basic language. Many compilers exist e.g. Borland, Java etc. Compiler construction have few steps which are taught in this course. This lecture includes: Running, Scanner, Compiler, Main, Void, Return, Int, Ctype, Studio, Tokdefs, C

Typology: Study notes

2011/2012

Uploaded on 08/04/2012

qader.khan
qader.khan 🇵🇰

4.3

(6)

12 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sohail Aslam Compiler Construction Notes
1
L
Le
ec
ct
tu
ur
re
e
1
10
0
Running the Scanner
Here is the output of the scanner when executed and given the file main.cpp as input,
i.e., the scanner is being asked to provide tokens found in the file main.cpp:
lex <main.cpp
259,void
258,main
283,(
284,)
285,{
258,FlexLexer
258,lex
290,;
260,int
258,tc
266,=
258,lex
291,.
258,yylex
283,(
284,)
290,;
263,while
283,(
258,tc
276,!=
257,0
284,)
258,cout
279,<<
258,tc
279,<<
292,","
279,<<
258,lex
291,.
258,YYText
283,(
284,)
279,<<
258,endl
290,;
258,tc
266,=
258,lex
291,.
258,yylex
283,(
284,)
290,;
286,}
pf3
pf4
pf5

Partial preview of the text

Download Running The Scanner-Compiler Construction-Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

Le Leccttuurree 1 100

Running the Scanner

Here is the output of the scanner when executed and given the file main.cpp as input,

i.e., the scanner is being asked to provide tokens found in the file main.cpp:

lex <main.cpp

259,void 258,main 283,( 284,) 285,{ 258,FlexLexer 258,lex 290,; 260,int 258,tc 266,= 258,lex 291,. 258,yylex 283,( 284,) 290,; 263,while 283,( 258,tc 276,!= 257, 284,) 258,cout 279,<< 258,tc 279,<< 292,"," 279,<< 258,lex 291,. 258,YYText 283,( 284,) 279,<< 258,endl 290,; 258,tc 266,= 258,lex 291,. 258,yylex 283,( 284,) 290,; 286,}

Flex input for C++

As an illustration of the power of Flex, here is the input for generating scanner for C++

compiler.

  • 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 '&'; }

"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; } [0-9]+{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; c2 = input(); } } 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

We now move the second module of the front-end: the parser. Recall the front-end

components:

scanner parser

source

code

tokens IR

errors