source: trunk/essentials/sys-devel/flex/examples/manual/front.y

Last change on this file was 3031, checked in by bird, 18 years ago

flex 2.5.33.

File size: 2.6 KB
Line 
1/* C code supplied at the beginning of the file. */
2
3%{
4
5#include <stdio.h>
6#include <string.h>
7
8extern int yylexlinenum; /* these are in YYlex */
9extern char *yytext; /* current token */
10
11
12%}
13
14/* Keywords and reserved words begin here. */
15
16%union{ /* this is the data union */
17 char name[128]; /* names */
18}
19
20/*-------------------- the reserved words -----------------------------*/
21
22%token PERIOD
23%token NEWLINE
24%token POSITIONAL
25
26%token VERB
27%token ADVERB
28
29%token PROPER_NOUN
30%token NOUN
31
32%token DECLARATIVE
33%token CONDITIONAL
34
35
36%type <name> declarative
37%type <name> verb_phrase
38%type <name> noun_phrase
39%type <name> position_phrase
40%type <name> adverb
41
42%type <name> POSITIONAL VERB ADVERB PROPER_NOUN
43%type <name> NOUN DECLARATIVE CONDITIONAL
44
45%%
46
47sentence_list : sentence
48 | sentence_list NEWLINE sentence
49 ;
50
51
52sentence : verb_phrase noun_phrase position_phrase adverb period
53 {
54 printf("I understand that sentence.\n");
55 printf("VP = %s \n",$1);
56 printf("NP = %s \n",$2);
57 printf("PP = %s \n",$3);
58 printf("AD = %s \n",$4);
59 }
60 | { yyerror("That's a strange sentence !!"); }
61 ;
62
63position_phrase : POSITIONAL declarative PROPER_NOUN
64 {
65 sprintf($$,"%s %s %s",$1,$2,$3);
66 }
67 | /* empty */ { strcpy($$,""); }
68 ;
69
70
71verb_phrase : VERB { strcpy($$,$1); strcat($$," "); }
72 | adverb VERB
73 {
74 sprintf($$,"%s %s",$1,$2);
75 }
76 ;
77
78adverb : ADVERB { strcpy($$,$1); }
79 | /* empty */ { strcpy($$,""); }
80 ;
81
82noun_phrase : DECLARATIVE NOUN
83 {
84 sprintf($$,"%s %s",$1,$2);
85 }
86 | CONDITIONAL declarative NOUN
87 {
88 sprintf($$,"%s %s %s",$1,$2,$3);
89 }
90 | NOUN { strcpy($$,$1); strcat($$," "); }
91 ;
92
93declarative : DECLARATIVE { strcpy($$,$1); }
94 | /* empty */ { strcpy($$,""); }
95 ;
96
97period : /* empty */
98 | PERIOD
99 ;
100
101
102%%
103
104/* Supplied main() and yyerror() functions. */
105
106int main(int argc, char *argv[])
107{
108 yyparse(); /* parse the file */
109 return(0);
110}
111
112int yyerror(char *message)
113{
114 extern FILE *yyout;
115
116 fprintf(yyout,"\nError at line %5d. (%s) \n",
117 yylexlinenum,message);
118}
Note: See TracBrowser for help on using the repository browser.