Skip to content

Commit

Permalink
Merge pull request fool2fish#30 from dxhj/master
Browse files Browse the repository at this point in the history
Add some source file for exercise 3.5
  • Loading branch information
fool2fish committed Aug 28, 2014
2 parents 99448e0 + 7b5a410 commit 3e865e8
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
48 changes: 48 additions & 0 deletions ch03/3.5/src/lex.l
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() {
/* ... */
}

35 changes: 35 additions & 0 deletions ch03/3.5/src/lex2.l
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;
}
}
40 changes: 40 additions & 0 deletions ch03/3.5/src/lex3.l
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()
*/
}

0 comments on commit 3e865e8

Please sign in to comment.