Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include <ctype.h>
- // Declare tokens
- #define IDENTIFIER 1
- #define INTEGER 2
- #define FLOAT 3
- #define KEYWORD 4
- #define OPERATOR 5
- #define PARENTHESIS 6
- void yyerror(const char *msg);
- %}
- %%
- /* Keywords */
- "var" { printf("KEYWORD: var\n"); return KEYWORD; }
- "if" { printf("KEYWORD: if\n"); return KEYWORD; }
- "then" { printf("KEYWORD: then\n"); return KEYWORD; }
- "else" { printf("KEYWORD: else\n"); return KEYWORD; }
- "begin" { printf("KEYWORD: begin\n"); return KEYWORD; }
- "end" { printf("KEYWORD: end\n"); return KEYWORD; }
- "or" { printf("KEYWORD: or\n"); return KEYWORD; }
- "and" { printf("KEYWORD: and\n"); return KEYWORD; }
- /* Identifiers */
- [a-zA-Z_][a-zA-Z0-9_]* { printf("IDENTIFIER: %s\n", yytext); return IDENTIFIER; }
- /* Numbers */
- [0-9]+ { printf("INTEGER: %s\n", yytext); return INTEGER; }
- [0-9]+"."[0-9]+ { printf("FLOAT: %s\n", yytext); return FLOAT; }
- /* Arithmetic operators */
- "+"|"-"|"*"|"/" { printf("OPERATOR: %s\n", yytext); return OPERATOR; }
- /* Parentheses */
- "(" { printf("PARENTHESIS: %s\n", yytext); return PARENTHESIS; }
- ")" { printf("PARENTHESIS: %s\n", yytext); return PARENTHESIS; }
- /* Ignore whitespace */
- [ \t\n]+ { /* ignore whitespace */ }
- /* Error handling */
- . { printf("Unknown character: %s\n", yytext); }
- %%
- int yywrap() {
- return 1;
- }
- void yyerror(const char *msg) {
- fprintf(stderr, "%s\n", msg);
- }
- int main() {
- yylex();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement