Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %{
- #include <stdio.h>
- #include <string.h>
- int line = 1;
- int token_count = 0;
- %}
- %option noyywrap
- DIGIT [0-9]
- LETTER [a-zA-Z_]
- WHITESPACE [ \n\r\t]
- %%
- {WHITESPACE}+ { if(yytext[0] == '\n') line++; /* Ignora spatiile */ }
- "char" { printf("TYPE_CHAR: %s (linia %d)\n", yytext, line); token_count++; }
- "double" { printf("TYPE_DOUBLE: %s (linia %d)\n", yytext, line); token_count++; }
- "else" { printf("ELSE: %s (linia %d)\n", yytext, line); token_count++; }
- "if" { printf("IF: %s (linia %d)\n", yytext, line); token_count++; }
- "int" { printf("TYPE_INT: %s (linia %d)\n", yytext, line); token_count++; }
- "return" { printf("RETURN: %s (linia %d)\n", yytext, line); token_count++; }
- "struct" { printf("STRUCT: %s (linia %d)\n", yytext, line); token_count++; }
- "void" { printf("VOID: %s (linia %d)\n", yytext, line); token_count++; }
- "while" { printf("WHILE: %s (linia %d)\n", yytext, line); token_count++; }
- {DIGIT}+ {
- printf("INT: %s (linia %d)\n", yytext, line);
- token_count++;
- }
- {DIGIT}+("."{DIGIT}+)?([eE][+-]?{DIGIT}+)? |
- {DIGIT}+"."{DIGIT}+ {
- printf("DOUBLE: %s (linia %d)\n", yytext, line);
- token_count++;
- }
- '[^']' { printf("CHAR: %s (linia %d)\n", yytext, line); token_count++; }
- \"[^\"]*\" { printf("STRING: %s (linia %d)\n", yytext, line); token_count++; }
- "," { printf("COMMA: %s (linia %d)\n", yytext, line); token_count++; }
- ";" { printf("SEMICOLON: %s (linia %d)\n", yytext, line); token_count++; }
- "(" { printf("LPAR: %s (linia %d)\n", yytext, line); token_count++; }
- ")" { printf("RPAR: %s (linia %d)\n", yytext, line); token_count++; }
- "[" { printf("LBRACKET: %s (linia %d)\n", yytext, line); token_count++; }
- "]" { printf("RBRACKET: %s (linia %d)\n", yytext, line); token_count++; }
- "{" { printf("LACC: %s (linia %d)\n", yytext, line); token_count++; }
- "}" { printf("RACC: %s (linia %d)\n", yytext, line); token_count++; }
- <<EOF>> { printf("END: EOF (linia %d)\n", line); token_count++; return 0; }
- "+" { printf("ADD: %s (linia %d)\n", yytext, line); token_count++; }
- "-" { printf("SUB: %s (linia %d)\n", yytext, line); token_count++; }
- "*" { printf("MUL: %s (linia %d)\n", yytext, line); token_count++; }
- "/" { printf("DIV: %s (linia %d)\n", yytext, line); token_count++; }
- "." { printf("DOT: %s (linia %d)\n", yytext, line); token_count++; }
- "&&" { printf("AND: %s (linia %d)\n", yytext, line); token_count++; }
- "||" { printf("OR: %s (linia %d)\n", yytext, line); token_count++; }
- "!" { printf("NOT: %s (linia %d)\n", yytext, line); token_count++; }
- "=" { printf("ASSIGN: %s (linia %d)\n", yytext, line); token_count++; }
- "==" { printf("EQUAL: %s (linia %d)\n", yytext, line); token_count++; }
- "!=" { printf("NOTEQ: %s (linia %d)\n", yytext, line); token_count++; }
- "<" { printf("LESS: %s (linia %d)\n", yytext, line); token_count++; }
- "<=" { printf("LESSEQ: %s (linia %d)\n", yytext, line); token_count++; }
- ">" { printf("GREATER: %s (linia %d)\n", yytext, line); token_count++; }
- ">=" { printf("GREATEREQ: %s (linia %d)\n", yytext, line); token_count++; }
- [a-zA-Z_][a-zA-Z0-9_]* {
- printf("ID: %s (linia %d)\n", yytext, line);
- token_count++;
- }
- "//"[^\n\r\0]* { /* Ignora comentariile pe o linie */ }
- . {
- printf("EROARE LEXICALA la linia %d: caracter neasteptat '%s'\n",
- line, yytext);
- }
- %%
- int main(int argc, char **argv) {
- if(argc > 1) {
- if(!(yyin = fopen(argv[1], "r"))) {
- perror(argv[1]);
- return 1;
- }
- }
- yylex();
- printf("\nRaport:\n");
- printf("Total linii procesate: %d\n", line);
- printf("Total token-uri recunoscute: %d\n", token_count);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement