-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvariable.y
More file actions
35 lines (31 loc) · 699 Bytes
/
variable.y
File metadata and controls
35 lines (31 loc) · 699 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* yacc program for recognition of valid variable */
%{
void yyerror(char *);
#include <stdio.h>
#include <stdlib.h>
%}
/* yacc defitions */
%union {char* exp;}
%start lines
%token <exp> identifier
%token newline
%token exit_command
%type <exp> lines
/* Grammar Rules with Actions */
%%
lines:
| lines identifier newline {printf("Valid Expression %s\n>> ", $2);}
| lines exit_command {exit(EXIT_SUCCESS);}
;
%%
/* main function */
int main() {
printf("Checks a valid variable\n");
printf("Reserved keywords: exit, quit\n");
printf(">> ");
return yyparse();
}
void yyerror(char *s) {
fprintf(stderr, "%s\n", s);
exit(1);
}