graph {
node A;
node B;
node C;
node D;
edge A -> B;
edge B -> C;
edge C -> A,B,D;
}
Write simple Flex regular expressions along with a Bison grammar that accepts inputs of this form, where edges can be directed from a node to any number of other nodes as shown in the example above.
p.
forall X function(X).
some X likes(X, john).
forall X some Y parent(X, Y).
Here, predicate names and constants are p, john, alice. Variables are X and Y.
%union {
char* str;
AST* node;
}
%token <str> WORD
%token PIPE
%type <node> line pipeline command
%%
line
: pipeline { $$ = $1; }
;
pipeline
: command { $$ = $1; }
| pipeline PIPE command { $$ = make_pipe($1, $3); }
;
command
: WORD { $$ = make_cmd($1, NULL); }
| WORD WORD { $$ = make_cmd($1, $2); }
;
For each valid input below, draw the entire parse tree using an LALR approach (carefully label all tokens and variables). Then, draw the abstract syntax tree created after parsing the entire input.
ls
ls file
cat file | grep foo | write a
x = 10;
y = 0;
z = x * 1;
a = z + 0;
b = 3 * 4;
c = b + 2;
d = c;
e = d;
if (true && (x > 0)) {
p = a;
} else {
p = 999;
}
q = p;
u = 5;
u = 6;
while (y < 3) {
m = b * 2;
n = x + 0;
t = m;
y = y + 1;
r = n + y;
}
return q;
s = 100;