xref: /minix3/external/bsd/byacc/dist/test/err_syntax27.y (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: err_syntax27.y,v 1.1.1.1 2015/01/03 22:58:23 christos Exp $	*/
2*0a6a1f1dSLionel Sambuc 
3*0a6a1f1dSLionel Sambuc %pure-parser
4*0a6a1f1dSLionel Sambuc 
5*0a6a1f1dSLionel Sambuc %parse-param { int regs[26]
6*0a6a1f1dSLionel Sambuc %parse-param { int *base
7*0a6a1f1dSLionel Sambuc 
8*0a6a1f1dSLionel Sambuc %lex-param { int *base
9*0a6a1f1dSLionel Sambuc 
10*0a6a1f1dSLionel Sambuc %{
11*0a6a1f1dSLionel Sambuc # include <stdio.h>
12*0a6a1f1dSLionel Sambuc # include <ctype.h>
13*0a6a1f1dSLionel Sambuc 
14*0a6a1f1dSLionel Sambuc #ifdef YYBISON
15*0a6a1f1dSLionel Sambuc #define YYSTYPE int
16*0a6a1f1dSLionel Sambuc #define YYLEX_PARAM base
17*0a6a1f1dSLionel Sambuc #define YYLEX_DECL() yylex(YYSTYPE *yylval, int *YYLEX_PARAM)
18*0a6a1f1dSLionel Sambuc #define YYERROR_DECL() yyerror(int regs[26], int *base, const char *s)
19*0a6a1f1dSLionel Sambuc int YYLEX_DECL();
20*0a6a1f1dSLionel Sambuc static void YYERROR_DECL();
21*0a6a1f1dSLionel Sambuc #endif
22*0a6a1f1dSLionel Sambuc 
23*0a6a1f1dSLionel Sambuc %}
24*0a6a1f1dSLionel Sambuc 
25*0a6a1f1dSLionel Sambuc %start list
26*0a6a1f1dSLionel Sambuc 
27*0a6a1f1dSLionel Sambuc %token DIGIT LETTER
28*0a6a1f1dSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc %left '|'
30*0a6a1f1dSLionel Sambuc %left '&'
31*0a6a1f1dSLionel Sambuc %left '+' '-'
32*0a6a1f1dSLionel Sambuc %left '*' '/' '%'
33*0a6a1f1dSLionel Sambuc %left UMINUS   /* supplies precedence for unary minus */
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc %% /* beginning of rules section */
36*0a6a1f1dSLionel Sambuc 
37*0a6a1f1dSLionel Sambuc list  :  /* empty */
38*0a6a1f1dSLionel Sambuc       |  list stat '\n'
39*0a6a1f1dSLionel Sambuc       |  list error '\n'
40*0a6a1f1dSLionel Sambuc             {  yyerrok ; }
41*0a6a1f1dSLionel Sambuc       ;
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc stat  :  expr
44*0a6a1f1dSLionel Sambuc             {  printf("%d\n",$1);}
45*0a6a1f1dSLionel Sambuc       |  LETTER '=' expr
46*0a6a1f1dSLionel Sambuc             {  regs[$1] = $3; }
47*0a6a1f1dSLionel Sambuc       ;
48*0a6a1f1dSLionel Sambuc 
49*0a6a1f1dSLionel Sambuc expr  :  '(' expr ')'
50*0a6a1f1dSLionel Sambuc             {  $$ = $2; }
51*0a6a1f1dSLionel Sambuc       |  expr '+' expr
52*0a6a1f1dSLionel Sambuc             {  $$ = $1 + $3; }
53*0a6a1f1dSLionel Sambuc       |  expr '-' expr
54*0a6a1f1dSLionel Sambuc             {  $$ = $1 - $3; }
55*0a6a1f1dSLionel Sambuc       |  expr '*' expr
56*0a6a1f1dSLionel Sambuc             {  $$ = $1 * $3; }
57*0a6a1f1dSLionel Sambuc       |  expr '/' expr
58*0a6a1f1dSLionel Sambuc             {  $$ = $1 / $3; }
59*0a6a1f1dSLionel Sambuc       |  expr '%' expr
60*0a6a1f1dSLionel Sambuc             {  $$ = $1 % $3; }
61*0a6a1f1dSLionel Sambuc       |  expr '&' expr
62*0a6a1f1dSLionel Sambuc             {  $$ = $1 & $3; }
63*0a6a1f1dSLionel Sambuc       |  expr '|' expr
64*0a6a1f1dSLionel Sambuc             {  $$ = $1 | $3; }
65*0a6a1f1dSLionel Sambuc       |  '-' expr %prec UMINUS
66*0a6a1f1dSLionel Sambuc             {  $$ = - $2; }
67*0a6a1f1dSLionel Sambuc       |  LETTER
68*0a6a1f1dSLionel Sambuc             {  $$ = regs[$1]; }
69*0a6a1f1dSLionel Sambuc       |  number
70*0a6a1f1dSLionel Sambuc       ;
71*0a6a1f1dSLionel Sambuc 
72*0a6a1f1dSLionel Sambuc number:  DIGIT
73*0a6a1f1dSLionel Sambuc          {  $$ = $1; (*base) = ($1==0) ? 8 : 10; }
74*0a6a1f1dSLionel Sambuc       |  number DIGIT
75*0a6a1f1dSLionel Sambuc          {  $$ = (*base) * $1 + $2; }
76*0a6a1f1dSLionel Sambuc       ;
77*0a6a1f1dSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc %% /* start of programs */
79*0a6a1f1dSLionel Sambuc 
80*0a6a1f1dSLionel Sambuc #ifdef YYBYACC
81*0a6a1f1dSLionel Sambuc extern int YYLEX_DECL();
82*0a6a1f1dSLionel Sambuc #endif
83*0a6a1f1dSLionel Sambuc 
84*0a6a1f1dSLionel Sambuc int
main(void)85*0a6a1f1dSLionel Sambuc main (void)
86*0a6a1f1dSLionel Sambuc {
87*0a6a1f1dSLionel Sambuc     int regs[26];
88*0a6a1f1dSLionel Sambuc     int base = 10;
89*0a6a1f1dSLionel Sambuc 
90*0a6a1f1dSLionel Sambuc     while(!feof(stdin)) {
91*0a6a1f1dSLionel Sambuc 	yyparse(regs, &base);
92*0a6a1f1dSLionel Sambuc     }
93*0a6a1f1dSLionel Sambuc     return 0;
94*0a6a1f1dSLionel Sambuc }
95*0a6a1f1dSLionel Sambuc 
96*0a6a1f1dSLionel Sambuc #define UNUSED(x) ((void)(x))
97*0a6a1f1dSLionel Sambuc 
98*0a6a1f1dSLionel Sambuc static void
YYERROR_DECL()99*0a6a1f1dSLionel Sambuc YYERROR_DECL()
100*0a6a1f1dSLionel Sambuc {
101*0a6a1f1dSLionel Sambuc     UNUSED(regs); /* %parse-param regs is not actually used here */
102*0a6a1f1dSLionel Sambuc     UNUSED(base); /* %parse-param base is not actually used here */
103*0a6a1f1dSLionel Sambuc     fprintf(stderr, "%s\n", s);
104*0a6a1f1dSLionel Sambuc }
105*0a6a1f1dSLionel Sambuc 
106*0a6a1f1dSLionel Sambuc int
YYLEX_DECL()107*0a6a1f1dSLionel Sambuc YYLEX_DECL()
108*0a6a1f1dSLionel Sambuc {
109*0a6a1f1dSLionel Sambuc 	/* lexical analysis routine */
110*0a6a1f1dSLionel Sambuc 	/* returns LETTER for a lower case letter, yylval = 0 through 25 */
111*0a6a1f1dSLionel Sambuc 	/* return DIGIT for a digit, yylval = 0 through 9 */
112*0a6a1f1dSLionel Sambuc 	/* all other characters are returned immediately */
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc     int c;
115*0a6a1f1dSLionel Sambuc 
116*0a6a1f1dSLionel Sambuc     while( (c=getchar()) == ' ' )   { /* skip blanks */ }
117*0a6a1f1dSLionel Sambuc 
118*0a6a1f1dSLionel Sambuc     /* c is now nonblank */
119*0a6a1f1dSLionel Sambuc 
120*0a6a1f1dSLionel Sambuc     if( islower( c )) {
121*0a6a1f1dSLionel Sambuc 	*yylval = (c - 'a');
122*0a6a1f1dSLionel Sambuc 	return ( LETTER );
123*0a6a1f1dSLionel Sambuc     }
124*0a6a1f1dSLionel Sambuc     if( isdigit( c )) {
125*0a6a1f1dSLionel Sambuc 	*yylval = (c - '0') % (*base);
126*0a6a1f1dSLionel Sambuc 	return ( DIGIT );
127*0a6a1f1dSLionel Sambuc     }
128*0a6a1f1dSLionel Sambuc     return( c );
129*0a6a1f1dSLionel Sambuc }
130