source: trunk/flex/examples/manual/pascal.lex@ 3032

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

flex 2.5.33.

File size: 3.1 KB
Line 
1/*
2 * pascal.lex: An example PASCAL scanner
3 *
4 */
5
6%{
7#include <stdio.h>
8#include "y.tab.h"
9
10int line_number = 0;
11
12void yyerror(char *message);
13
14%}
15
16%x COMMENT1 COMMENT2
17
18white_space [ \t]*
19digit [0-9]
20alpha [A-Za-z_]
21alpha_num ({alpha}|{digit})
22hex_digit [0-9A-F]
23identifier {alpha}{alpha_num}*
24unsigned_integer {digit}+
25hex_integer ${hex_digit}{hex_digit}*
26exponent e[+-]?{digit}+
27i {unsigned_integer}
28real ({i}\.{i}?|{i}?\.{i}){exponent}?
29string \'([^'\n]|\'\')+\'
30bad_string \'([^'\n]|\'\')+
31
32%%
33
34"{" BEGIN(COMMENT1);
35<COMMENT1>[^}\n]+
36<COMMENT1>\n ++line_number;
37<COMMENT1><<EOF>> yyerror("EOF in comment");
38<COMMENT1>"}" BEGIN(INITIAL);
39
40"(*" BEGIN(COMMENT2);
41<COMMENT2>[^)*\n]+
42<COMMENT2>\n ++line_number;
43<COMMENT2><<EOF>> yyerror("EOF in comment");
44<COMMENT2>"*)" BEGIN(INITIAL);
45<COMMENT2>[*)]
46
47 /* note that FILE and BEGIN are already
48 * defined in FLEX or C so they can't
49 * be used. This can be overcome in
50 * a cleaner way by defining all the
51 * tokens to start with TOK_ or some
52 * other prefix.
53 */
54
55and return(AND);
56array return(ARRAY);
57begin return(_BEGIN);
58case return(CASE);
59const return(CONST);
60div return(DIV);
61do return(DO);
62downto return(DOWNTO);
63else return(ELSE);
64end return(END);
65file return(_FILE);
66for return(FOR);
67function return(FUNCTION);
68goto return(GOTO);
69if return(IF);
70in return(IN);
71label return(LABEL);
72mod return(MOD);
73nil return(NIL);
74not return(NOT);
75of return(OF);
76packed return(PACKED);
77procedure return(PROCEDURE);
78program return(PROGRAM);
79record return(RECORD);
80repeat return(REPEAT);
81set return(SET);
82then return(THEN);
83to return(TO);
84type return(TYPE);
85until return(UNTIL);
86var return(VAR);
87while return(WHILE);
88with return(WITH);
89
90"<="|"=<" return(LEQ);
91"=>"|">=" return(GEQ);
92"<>" return(NEQ);
93"=" return(EQ);
94
95".." return(DOUBLEDOT);
96
97{unsigned_integer} return(UNSIGNED_INTEGER);
98{real} return(REAL);
99{hex_integer} return(HEX_INTEGER);
100{string} return{STRING};
101{bad_string} yyerror("Unterminated string");
102
103{identifier} return(IDENTIFIER);
104
105[*/+\-,^.;:()\[\]] return(yytext[0]);
106
107{white_space} /* do nothing */
108\n line_number += 1;
109. yyerror("Illegal input");
110
111%%
112
113void yyerror(char *message)
114{
115 fprintf(stderr,"Error: \"%s\" in line %d. Token = %s\n",
116 message,line_number,yytext);
117 exit(1);
118}
119
120
Note: See TracBrowser for help on using the repository browser.