




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





CS143 Handout 16
Summer 2012 July 11th, 2012
Handout written by Maggie Johnson and revised by Julie Zelenski.
digit – > 0 {digit.value = 0} | 1 {digit.value = 1} | 2 {digit.value = 2} ... | 9 {digit.value = 9}
int1 – > digit {int1.value = digit.value} | int2 digit {int1.value = int2.value*10 + digit.value}
X – > Y 1 Y 2 ...Yn
X.a = f(Y1.a, Y2.a, ...Yn.a)
X – > Y 1 Y 2 ...Yn
Yk.a = f(X.a, Y1.a, Y2.a, ..., Yk-1.a, Yk+1.a, ..., Yn.a)
D – > var V; D | S – > V := E; S | V – > x | y | z
int value = 4*10 + 2 = 42
int digit value = 2
value = 4
digit
value = 4
4
2
Session – > CommandList T_QUIT CommandList – > Command CommandList | Command – > Login | Get | Logout Login – > User Pass User – > T_USER T_IDENT Pass – > T_PASS T_IDENT Get – > T_GET T_IDENT MoreFiles MoreFiles – > T_IDENT MoreFiles | Logout – > T_LOGOUT
static void ParseCommands() { Connection *conn = NULL; int next;
while ((next = GetLookahead()) != T_QUIT) { switch (next) { case T_USER: conn = ParseLogin(); break; case T_GET: ParseGet(conn); break; case T_LOGOUT: ParseLogout(conn); conn = NULL; break; default: ReportParseFailure("command expected", yytext); } } }
static Connection *ParseLogin() // returns attribute of opened connection { char *user = ParseUser(); char *pass = ParsePassword(); return Login(user, pass); // uses attributes passed up from below }
static char *ParseUser() // returns attribute of username given { MatchToken(T_USER); char *str = ParseIdentifier(); // gets name from identfier child node MatchToken('\n'); return str;
static char *ParsePassword() // similar to username { MatchToken(T_PASS); char *str = ParseIdentifier(); MatchToken('\n'); return str; }
static char *ParseIdentifier() { char *str = NULL;
if (GetLookahead() == T_IDENT) { str = strdup(yytext); MatchToken(T_IDENT); }
return str; // returns NULL on error }
static void ParseGet(Connection *conn) // receives conn from above { MatchToken(T_GET); char *filename = ParseIdentifier(); MatchToken('\n'); Transfer(conn, filename);// reports error if conn NULL }
static void ParseLogout(Connection *conn) // receives conn from above { MatchToken(T_LOGOUT); MatchToken('\n'); Logout(conn); // reports error if conn NULL }
static int Lookup(const char *name); static void Store(const char *name, int val); %}
%union { int intVal; char name[32]; }
%token
%type
%left '+' '-' %left '*' '/' %right U_minus
S : Stmt | S Stmt ;
Stmt : T_Identifier '=' E '\n' { Store($1, $3); printf("%d\n", $3);} | E '\n' { printf("%d\n", $1);} | error '\n' { printf("Discarding malformed expression.\n");} ;
{ if ($3 == 0) { printf("divide by zero\n"); $$ = 0;
} else $$ = $1 / $3;} | '(' E ')' { $$ = $2;} | '-' E %prec U_minus { $$ = -$2;} | T_Int { $$ = $1;} | T_Identifier { $$ = Lookup($1);} ; %%
/* Store/Lookup functions omitted for clarify They are just ordinary hash table operations */
#include "y.tab.h" %}
[0-9]+ { yylval.intVal = atoi(yytext); return T_Int;} [a-zA-Z]+ { strncpy(yylval.name, yytext, 32); return T_Identifier;} [-+/()\n=] { return yytext[0];} [ \t] { /* ignore whitespace */ }