Untitled
%{ #include <stdio.h> #include <stdlib.h> int rows = 1, cols = 1; // To store dimensions %} %token INT FLOAT ARRAY END %% declaration: type arrays END { printf("Type: %s\n", $1); printf("Dimensions: %d x %d\n", rows, cols); } ; type: INT { $$ = "int"; } FLOAT { $$ = "float"; } ; arrays: ARRAY arrays { rows *= atoi(yytext); cols *= atoi(yytext); } | /* empty */ ; %% int main() { printf("Enter array declaration (e.g., int[5][10]):\n"); yyparse(); return 0; } int yyerror(const char *s) { fprintf(stderr, "Error: %s\n", s); return 1; } %{ #include "y.tab.h" %} %% [int]+ { return INT; } [float]+ { return FLOAT; } "["[0-9]+"]" { return ARRAY; } \n { return END; } [ \t] { /* ignore whitespace */ } . { /* ignore any other character */ } %%
Leave a Comment