forked from fool2fish/dragon-book-exercise-answers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request fool2fish#30 from dxhj/master
Add some source file for exercise 3.5
- Loading branch information
Showing
3 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
%{ | ||
/* definitions of manifest constants | ||
LT, LE, EQ, NE, GT, GE, | ||
IF, THEN, ELSE, ID, NUMBER, RELOP */ | ||
%} | ||
/* regular definitions */ | ||
delim [ \t\n] | ||
ws {delim}+ | ||
|
||
/* Exercise 3.5.1 - c) We can allow the underscore here, but I think it's better in the ID definition. */ | ||
letter [A-Za-z] | ||
digit [0-9] | ||
|
||
/* Exercise 3.5.1 - c) */ | ||
id {letter}({letter}|{digit}|_)* | ||
|
||
number {digit}+(\.{digit}+)?(E[+-]?{digit }+)? | ||
doubleq \" | ||
|
||
%% | ||
{ws} {/* no action and no return */} | ||
if {return (IF); } | ||
while { return (WHILE); /* Exercise 3.5.1 - a) */ } | ||
then {return (THEN); } | ||
else {return (ELSE) ; } | ||
{id} {yylval = (int) installID(); return (ID); } | ||
{doubleq}.*{doubleq} { yylval = (int) installString(); return (STRING); /* Exercise 3.5.1 - d) */} | ||
{number} {yylval = (int) installNum(); return (NUMBER); } | ||
"<" {yylval = LT; return (RELOP); } | ||
"<=" {yylval = LE; return (RELOP);} | ||
"==" {yylval = EQ; return (RELOP); /* Exercise 3.5.1 - b) */ } | ||
"!=" {yylval = NE; return (RELOP ); /* Exercise 3.5.1 - b) */ } | ||
">" {yylval = GT; return (RELOP); } | ||
">=" {yylval = GE; return (RELOP); } | ||
|
||
%% | ||
|
||
int installID() { /* function to install the lexeme, whose first character is pointed to bu yytext, | ||
and whose length is yyleng, into the symbol table and return a pointer thereto */ | ||
} | ||
|
||
int installNum() { /* similar to installID, but puts numerical constants into a seperate table */ | ||
} | ||
|
||
int installString() { | ||
/* ... */ | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
%{ | ||
%} | ||
|
||
delim [ \t] | ||
ws {delim}+ | ||
letter [a-zA-Z] | ||
word {letter}+ | ||
|
||
%% | ||
{ws} { printf("%s", yytext); } | ||
{word} { | ||
if (starts_vowel(yytext)) | ||
printf("%say", yytext); | ||
else | ||
printf("%s%cay", yytext+1, yytext[0]); | ||
. { printf("%s", yytext); } | ||
%% | ||
|
||
int starts_vowel(char *c){ | ||
switch(c[0]){ | ||
case 'a': | ||
case 'e': | ||
case 'i': | ||
case 'o': | ||
case 'u': | ||
case 'A': | ||
case 'E': | ||
case 'I': | ||
case 'O': | ||
case 'U': | ||
return 1; | ||
default: | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
%{ | ||
|
||
%} | ||
|
||
letter [a-zA-Z] | ||
digit [0-9] | ||
|
||
/* [w-W][h-H][i-I][l-L][e-E] works too. */ | ||
while (?i:while) | ||
|
||
from (?i:from) | ||
select (?i:select) | ||
id {letter}({letter}|{digit})* | ||
|
||
%% | ||
{while} { printf("WHILE KEYWORD"); return (WHILE); } | ||
{from} { printf("FROM KEYWORD"); return (FROM); } | ||
{select} { printf("SELECT KEYWORD"); return (SELECT); } | ||
{id} { printf("ID"); yytext = lowercase(yytext); yylval = (int) installID(); return (ID); } | ||
%% | ||
|
||
|
||
int installID(){ | ||
/* We've to convert the lexeme to lowercase and install it | ||
example: | ||
yytext: foO | ||
yytext = lowercase(yytext) | ||
installID() | ||
yytext: Strstr | ||
yytext = lowercase(yytext) | ||
yylval = (int) installID() | ||
yytext: strstr (already exists in the symbol table) | ||
yytext = lowercase(yytext) | ||
yylval = (int) installID() | ||
*/ | ||
} |