flex代写 Project #1. Scanner

Project #1. Scanner 2018

Hanyang University Division of Computer Science & Engineering

Scanner

• Implementation of C-scanner (both 2 methods)

– Implementation of C-Scanner using C-code

(modify Tiny compiler code)
• globals.h main.c util.h util.c scan.h scan.c

– Implementation of C-Scanner using lex(flex) by Tiny.l modification

Hanyang University Division of Computer Science & Engineering

Lexical Convention of C-Minus • Keyword

else if int return void while (lower case)

• Symbol
+ – * / < <= > >= == != = ; ,

( ) [ ] { } /* */

• Token
𝐼𝐼𝐼𝐼 = 𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙 𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙*

𝑁𝑁𝑁𝑁𝑁𝑁 = 𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑙𝑙 𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑙𝑙 * 𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙𝑙 = a | … | z | A | … | Z 𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑑𝑙𝑙 = 0 | 1 | … | 9

Hanyang University Division of Computer Science & Engineering

Lexical Convention of C-Minus • White space:

– Ignore other cases except WS between 𝐼𝐼𝐼𝐼, 𝑁𝑁𝑁𝑁𝑁𝑁, and keywords (ex: beginning and end of line)

• Comments(/*…*/)follownormalCnotation. – Cannot be nested.

• Please see “Kenneth C. Louden book p. 491-492”

Hanyang University Division of Computer Science & Engineering

Hint: where to see?

• globals.h
– TokenType should be modified for C-Minus

Hanyang University Division of Computer Science & Engineering

Hint: where to see?

• main.c
– To meet scanner project goal
– NO_PARSE, EchoSource, TraceScan

Hanyang University Division of Computer Science & Engineering

Hint: where to see?

• scan.c
– Need to add states for C-Minus DFA

Hanyang University Division of Computer Science & Engineering

Hint: where to see?

• scan.c
– Reserved word should be added for C-Minus

Hanyang University Division of Computer Science & Engineering

Hint: where to see?

• scan.c
– Need to modify getToken for C-Minus

Hanyang University Division of Computer Science & Engineering

Hint: where to see?

• util.c
– Need to modify printToken() for C-Minus

Hanyang University Division of Computer Science & Engineering

Example– Tiny compiler modification

/* A program to perform Euclid’s Algorithm to computer gcd */

int gcd (int u, int v)

{
if (v == 0) return u;
else return gcd(v,u-u/v*v); /* u-u/v*v == u mod v */

}

void main(void)

{
int x; int y;
x = input(); y = input(); output(gcd(x,y));

}

Hanyang University Division of Computer Science & Engineering

Scanner

• Implementation of C-scanner (both 2 methods)

– Implementation of C-Scanner using C-code

(modify Tiny compiler code)
• globals.h main.c util.h util.c scan.h scan.c

– Implementation of C-Scanner using lex(flex) by Tiny.l modification

Hanyang University Division of Computer Science & Engineering

lex / flex

  • Lexeme analysis
  • Automatically generate a target scanner based on input Res
  • Work with yacc (bison)
  • http://flex.sourceforge.net/

    – Manual: http://flex.sourceforge.net/manual/

Hanyang University Division of Computer Science & Engineering

lex environment • Ubuntu 14.04 기준:

– apt-get install flex

• Usage

– tiny.l (in Tiny source) should be modified

– flex <Lex Filename>
• ex) flex cminus.l
• lex.yy.c will be created

• Output can be different

– It’s okay if the output is somewhat different from the previous work.

Hanyang University Division of Computer Science & Engineering

Hint: where to see?

• globals.h, main.c, util.c
– Same as manual implementation

• scan.c
– This file is not used because getToken() is

automatically generated using flex

Hanyang University Division of Computer Science & Engineering

Hint: where to see?

• cminus.l
– Should be created for C-Minus using tiny.l

Hanyang University Division of Computer Science & Engineering

/* A program to perform Euclid’s Algorithm to computer gcd */

int gcd (int u, int v)

{
if (v == 0) return u;
else return gcd(v,u-u/v*v); /* u-u/v*v == u mod v */

}

void main(void)

{
int x; int y;
x = input(); y = input(); output(gcd(x,y));

}

Example – Flex

Hanyang University Division of Computer Science & Engineering

Compilation

• Use the Makefile!
– Use Tiny compiler Makefile
– The Makefile should be modificed

Hanyang University Division of Computer Science & Engineering

Compilation using flex • Compilation using flex

– Following code should be added to Makefile

• ‘-lfl’: must be added
• ‘cminus.l’ should exist in the same folder with other header files. • OS X: -ll

Hanyang University Division of Computer Science & Engineering

Report

• Guideline (~5pages)
– Compilation method and environment

– Explanation about how to implement and how to operate – Example and Result Screenshot

• File format
– MS Word, HWP, PDF, …

Hanyang University Division of Computer Science & Engineering

Submission

• questions compiler.teachingassistant@gmail.com

• Scanner submission deadline – 10/14(Sun) 23:59:59

Hanyang University Division of Computer Science & Engineering

Contact (Prof. Yongjun Park) • Submission

  • –  Where: Using GitLab
    • https://hconnect.hanyang.ac.kr
    • GitProject:https://hconnect.hanyang.ac.kr/2018_ELE4029_10734/2018_ELE4029_Student#.git
    • ExampleURL:

      https://hconnect.hanyang.ac.kr/2018_ELE4029_10734/2018_ELE4029_2018001234.git

  • –  Teaching Assistant
    • compiler.teachingassistant@gmail.com
    • If you don’t have the GITLAB account, please let him know the account information after creation.

– What to submit
• All the source codes and the report

Hanyang University Division of Computer Science & Engineering

Q&A

Hanyang University Division of Computer Science & Engineering