Skip to content

Commit 0b11d61

Browse files
committed
first commit
0 parents  commit 0b11d61

File tree

244 files changed

+3465
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

244 files changed

+3465
-0
lines changed

Makefile

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
SCANNER = scanner
2+
PARSER = parser
3+
CC = gcc
4+
CFLAGS = -Wall -std=c11
5+
LEX = flex
6+
YACC = bison
7+
LIBS = -lfl
8+
9+
EXEC = $(PARSER)
10+
OBJS = $(PARSER) \
11+
$(SCANNER)
12+
OBJS := $(OBJS:=.o)
13+
DEPS := $(OBJS:=.d)
14+
15+
all: $(EXEC)
16+
17+
$(SCANNER): $(SCANNER).c
18+
$(CC) $(CCFLAGS) $< -o $(SCANNER) $(LIBS)
19+
20+
$(SCANNER).c: %.c: %.l
21+
$(LEX) -o $@ $<
22+
23+
$(PARSER).c: %.c: %.y
24+
$(YACC) -d -o $@ -v $<
25+
26+
$(EXEC): $(OBJS)
27+
$(CC) -o $@ $^ $(LIBS)
28+
29+
prepare:
30+
# we have installed the package meet the basic needs
31+
# you can prepare the extra package you need here, e.g.
32+
#apt install clang
33+
#apt install g++
34+
35+
test: all
36+
make test -C test/
37+
38+
check: all
39+
make check -C test/
40+
41+
pack:
42+
make clean
43+
zip -r icd20-hw2.zip . -x ".*" -x "*.zip" -x "test/*"
44+
45+
.PHONY: clean
46+
47+
clean:
48+
make clean -C test/
49+
$(RM) $(SCANNER) $(SCANNER:=.c) $(PARSER:=.c) $(PARSER:=.h) $(DEPS) $(PARSER:=.output) $(OBJS) $(EXEC)
50+
51+
DOCKERHUB_ACCOUNT=plaslab
52+
IMAGE_NAME = compiler-f20-hw2
53+
DOCKER_IMG = ${DOCKERHUB_ACCOUNT}/${IMAGE_NAME}:latest
54+
55+
pull:
56+
docker pull ${DOCKER_IMG}

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# ICD20 Homework2
2+
3+
## How port your homework1 to homework2
4+
5+
1. remove the main from the scanner.l
6+
2. remove all the `#define [TOKEN] [INDEX]` in `scanner.l` (we define them in parser.y instead)
7+
3. add `#include "parser.h"` in your `scanner.l`
8+
4. add a `#pragma token on` and `#pragma token off` to control `[INFO]` and `token(type:...)...` message, default is off

activate.bat

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
chcp 65001
2+
docker run --rm -it -w /root -v "%cd%":/root/hw2 plaslab/compiler-f20-hw2:latest /bin/bash

activate.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
docker run --rm -it -w /root -v "${PWD}":/root/hw2 plaslab/compiler-f20-hw2:latest /bin/bash

cscc-activate.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dosh run plaslab/compiler-f20-hw2:latest /bin/bash

mobapkg.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
yes | /bin/MobaBox.exe apt-get install make
2+
yes | /bin/MobaBox.exe apt-get install flex
3+
yes | /bin/MobaBox.exe apt-get install gcc-core
4+
yes | /bin/MobaBox.exe apt-get install m4
5+
yes | /bin/MobaBox.exe apt-get install zip
6+
yes | /bin/MobaBox.exe apt-get apt-get install bison

parser.y

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
%{
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include <stdint.h>
5+
6+
#include "error.h"
7+
8+
#define MAX_LINE_LENG 256
9+
extern int line_no, chr_no, opt_list;
10+
extern char buffer[MAX_LINE_LENG];
11+
extern FILE *yyin; /* declared by lex */
12+
extern char *yytext; /* declared by lex */
13+
extern int yyleng;
14+
15+
extern int yylex(void);
16+
static void yyerror(const char *msg);
17+
%}
18+
19+
%token PROGRAM VAR ARRAY OF INTEGER REAL STRING FUNCTION PROCEDURE PBEGIN END IF THEN ELSE WHILE DO NOT AND OR
20+
21+
%token LPAREN RPAREN SEMICOLON DOT COMMA COLON LBRACE RBRACE DOTDOT ASSIGNMENT ADDOP SUBOP MULOP DIVOP LTOP GTOP EQOP GETOP LETOP NEQOP
22+
23+
%token IDENTIFIER REALNUMBER INTEGERNUM SCIENTIFIC LITERALSTR
24+
25+
%union {
26+
int val;
27+
char* text;
28+
double dval;
29+
}
30+
31+
32+
%%
33+
34+
/* define your snytax here */
35+
prog: PROGRAM
36+
;
37+
38+
%%
39+
40+
void yyerror(const char *msg) {
41+
fprintf(stderr,
42+
"[ERROR] line %4d:%3d %s, Unmatched token: %s\n",
43+
line_no, chr_no-(int)yyleng+1, buffer, yytext);
44+
}
45+
46+
int main(int argc, const char *argv[]) {
47+
48+
if(argc > 2)
49+
fprintf( stderr, "Usage: ./parser [filename]\n" ), exit(0);
50+
51+
FILE *fp = argc == 1 ? stdin : fopen(argv[1], "r");
52+
53+
if(fp == NULL)
54+
fprintf( stderr, "Open file error\n" ), exit(-1);
55+
56+
yyin = fp;
57+
yyparse();
58+
return 0;
59+
}

scanner.l

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
%{
2+
/*
3+
* scanner.l
4+
*
5+
* lex input file for pascal scanner
6+
*
7+
*/
8+
9+
#include <stdio.h>
10+
#include <string.h>
11+
#include "parser.h"
12+
13+
int fileno(FILE *);
14+
15+
#define YY_USER_ACTION chr_no += yyleng; // note this line
16+
17+
#define MAX_LINE_LENG 256
18+
#define LIST strcat(buffer, yytext)
19+
#define LIST_FLUSH do{ if(opt_list) printf("%s", buffer); *buffer = 0; }while(0)
20+
#define LOG(TYPE) do{ LIST; if(opt_token) fprintf(stderr, "token(type:%-10s) on line %4d, %3d : %s\n", #TYPE, line_no, chr_no - yyleng + 1, yytext); } while(0)
21+
22+
int opt_list = 1, opt_token = 0;
23+
int line_no = 1, chr_no = 0;
24+
char buffer[MAX_LINE_LENG];
25+
26+
%}
27+
28+
%option nounput
29+
%option noinput
30+
31+
A [aA]
32+
B [bB]
33+
C [cC]
34+
D [dD]
35+
E [eE]
36+
F [fF]
37+
G [gG]
38+
H [hH]
39+
I [iI]
40+
J [jJ]
41+
K [kK]
42+
L [lL]
43+
M [mM]
44+
N [nN]
45+
O [oO]
46+
P [pP]
47+
Q [qQ]
48+
R [rR]
49+
S [sS]
50+
T [tT]
51+
U [uU]
52+
V [vV]
53+
W [wW]
54+
X [xX]
55+
Y [yY]
56+
Z [zZ]
57+
58+
%%
59+
/* v could do something */
60+
{P}{R}{O}{G}{R}{A}{M} {LOG(KEYWORD); return(PROGRAM); }
61+
{V}{A}{R} {LOG(KEYWORD); return(VAR); }
62+
{A}{R}{R}{A}{Y} {LOG(KEYWORD); return(ARRAY); }
63+
{O}{F} {LOG(KEYWORD); return(OF); }
64+
{I}{N}{T}{E}{G}{E}{R} {LOG(KEYWORD); return(INTEGER); }
65+
{R}{E}{A}{L} {LOG(KEYWORD); return(REAL); }
66+
{S}{T}{R}{I}{N}{G} {LOG(KEYWORD); return(STRING); }
67+
{F}{U}{N}{C}{T}{I}{O}{N} {LOG(KEYWORD); return(FUNCTION); }
68+
{P}{R}{O}{C}{E}{D}{U}{R}{E} {LOG(KEYWORD); return(PROCEDURE); }
69+
{B}{E}{G}{I}{N} {LOG(KEYWORD); return(PBEGIN); }
70+
{E}{N}{D} {LOG(KEYWORD); return(END); }
71+
{I}{F} {LOG(KEYWORD); return(IF); }
72+
{T}{H}{E}{N} {LOG(KEYWORD); return(THEN); }
73+
{E}{L}{S}{E} {LOG(KEYWORD); return(ELSE); }
74+
{W}{H}{I}{L}{E} {LOG(KEYWORD); return(WHILE); }
75+
{D}{O} {LOG(KEYWORD); return(DO); }
76+
{N}{O}{T} {LOG(KEYWORD); return(NOT); }
77+
{A}{N}{D} {LOG(KEYWORD); return(AND); }
78+
{O}{R} {LOG(KEYWORD); return(OR); }
79+
80+
"(" {LOG(KEYWORD); return(LPAREN); }
81+
")" {LOG(KEYWORD); return(RPAREN); }
82+
";" {LOG(KEYWORD); return(SEMICOLON); }
83+
"." {LOG(KEYWORD); return(DOT); }
84+
"," {LOG(KEYWORD); return(COMMA); }
85+
":" {LOG(KEYWORD); return(COLON); }
86+
"[" {LOG(KEYWORD); return(LBRACE); }
87+
"]" {LOG(KEYWORD); return(RBRACE); }
88+
".." {LOG(KEYWORD); return(DOTDOT); }
89+
":=" {LOG(KEYWORD); return(ASSIGNMENT); }
90+
"+" {LOG(KEYWORD); return(ADDOP); }
91+
"-" {LOG(KEYWORD); return(SUBOP); }
92+
"*" {LOG(KEYWORD); return(MULOP); }
93+
"/" {LOG(KEYWORD); return(DIVOP); }
94+
">" {LOG(KEYWORD); return(GTOP); }
95+
"<" {LOG(KEYWORD); return(LTOP); }
96+
"=" {LOG(KEYWORD); return(EQOP); }
97+
">=" {LOG(KEYWORD); return(GETOP); }
98+
"<=" {LOG(KEYWORD); return(LETOP); }
99+
"!=" {LOG(KEYWORD); return(NEQOP); }
100+
101+
/* define identifier here */
102+
103+
/* define INTEGERNUM, REALNUMBER, SCIENTIFIC here */
104+
105+
/* define single/multiple line comment here */
106+
107+
/* define string constant (LITERALSTR) here */
108+
109+
/* define pragma here */
110+
111+
[ \t\f\r] LIST;
112+
113+
\n {
114+
LIST;
115+
LIST_FLUSH;
116+
line_no++, chr_no = 0;
117+
}
118+
119+
. { LIST; fprintf(stderr, "[ERROR] line %4d:%3d lexical analyzer error %s\n", line_no, chr_no-(int)yyleng+1, yytext); }

test/Makefile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
EXE := ../parser
2+
SRCS := $(wildcard */*.p)
3+
OBJS := $(patsubst %.p,output/%.token,$(SRCS))
4+
5+
all: output $(OBJS)
6+
7+
output/%.token: %.p
8+
@-mkdir -p output/$$(dirname $^)
9+
@-./$(EXE) $^ > $@.src 2>$@.info
10+
11+
test: all
12+
@bash test.sh
13+
14+
check: all
15+
@bash check.sh
16+
17+
output:
18+
mkdir -p $@
19+
20+
clean:
21+
$(RM) -rf output
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ERROR] line 5: 24 var d, e: array [ 1 +, Unmatched token: +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PROGRAM foo(input, output, error) ;
2+
// this test checks various degrees of constant folding
3+
4+
var a, b, c: integer;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ERROR] line 5: 9 var d+, Unmatched token: +
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PROGRAM foo(input, output, error) ;
2+
// this test checks various degrees of constant folding
3+
4+
var a, b, c: integer;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ERROR] line 5: 30 var d, e: array [ 1 .. 10 ;, Unmatched token: ;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PROGRAM foo(input, output, error) ;
2+
// this test checks various degrees of constant folding
3+
4+
var a, b, c: integer;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ERROR] line 9: 9 b 6, Unmatched token: 6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
PROGRAM foo(input, output, error) ;
2+
// this test checks various degrees of constant folding
3+
4+
var a, b, c: integer;
5+
var d, e: array [ 1 .. 10 ] of integer;
6+
7+
begin
8+
a := 2+99; // simple expression
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ERROR] line 5: 9 var d;, Unmatched token: ;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PROGRAM foo(input, output, error) ;
2+
// this test checks various degrees of constant folding
3+
4+
var a, b, c: integer;

test/answers/test-constant/test-constant.token.info

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
PROGRAM foo(input, output, error) ;
2+
// this test checks various degrees of constant folding
3+
4+
var a, b, c: integer;
5+
var d, e: array [ 1 .. 10 ] of integer;
6+
7+
begin
8+
a := 2+99; // simple expression
9+
b := 3 + 93 * 5 + 87 * (23+15*6); // complex expression
10+
c[4+2] := 3 + 9 * 5 + 87 * (2+15*6); // constant array index
11+
// constant address
12+
c[3 + 9 * 5 + 87 * (2+15*6)] := 3 + 9 * 5 + 8* (2+15*6)
13+
end. // this is the end of the program
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ERROR] line 13: 9 a b, Unmatched token: b
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
PROGRAM foo(input, output, error) ;
2+
var a, b: integer;
3+
4+
// test simple, non-recursive functions
5+
function simplesum(a: integer) : integer;
6+
begin
7+
simplesum := a * b
8+
// simplesum is the return value
9+
end;
10+
11+
begin
12+
a := 7; b := 13;

test/answers/test-function/1.mod.token.info

Whitespace-only changes.
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
PROGRAM foo(input, output, error) ;
2+
var a, b: integer;
3+
4+
// test simple, non-recursive functions
5+
function simplesum(a: integer) : integer;
6+
begin
7+
simplesum := a * b
8+
// simplesum is the return value
9+
end;
10+
11+
begin
12+
a := 7; b := 13;
13+
a := 3 + simplesum(10); // the result is .
14+
b := 1 * simplesum(-10) // the result is .
15+
end. // this is the end of the program
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ERROR] line 12: 13 a := 7(, Unmatched token: (
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
PROGRAM foo(input, output, error) ;
2+
var a, b: integer;
3+
4+
// test simple, non-recursive functions
5+
function simplesum(a: integer) : integer;
6+
begin
7+
simplesum := a * b
8+
// simplesum is the return value
9+
end;
10+
11+
begin
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ERROR] line 5: 14 function 3, Unmatched token: 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PROGRAM foo(input, output, error) ;
2+
var a, b: integer;
3+
4+
// test simple, non-recursive functions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[ERROR] line 5: 27 function simplesum(a: ;, Unmatched token: ;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PROGRAM foo(input, output, error) ;
2+
var a, b: integer;
3+
4+
// test simple, non-recursive functions

test/answers/test-function/test-function.token.info

Whitespace-only changes.

0 commit comments

Comments
 (0)