madvice

calc-ad

Jun 23rd, 2026 (edited)
42
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | Source Code | 0 0
  1. // calc-ad.l:
  2. integer         [0-9]+
  3. variable        [a-zA-Z]
  4.  
  5. %{
  6.     #include <stdio.h>
  7.     #include "y.tab.h"
  8.     void yyerror(char *msg, ...);  
  9. %}
  10.  
  11. %%
  12.  
  13. {integer}                { yylval.num = atoi(yytext); return INTEGER; }
  14. {variable}               { yylval.var = yytext[0]; return VARIABLE; }
  15. [+*()=;\n]               { return yytext[0]; }
  16. [ \t]                    { }
  17.  
  18. %%
  19.  
  20. int yywrap() {
  21.     return 1;
  22. }
  23.  
  24. // calc.y:
  25. %{
  26.     #include <stdio.h>
  27.     #include <stdlib.h>
  28.     #include <stdarg.h>
  29.     int yylex(void);
  30.     void yyerror(char *msg, ...);
  31.     void setvalue(char var, int val);
  32.     int getvalue(char var);
  33.     int symbol_table[52] = {0};
  34.     int declared[52] = {0};
  35. %}
  36.  
  37. %union { char var; int num; }
  38. %start statements
  39. %token <num> INTEGER
  40. %token <var> VARIABLE
  41. %type <num> E T F
  42.  
  43. %%
  44.  
  45. statements :
  46.               statements statement '\n'
  47.             |
  48.             ;
  49.  
  50. statement :
  51.               E ';'                 { printf("Value of expression: %d\n", $1); }
  52.             | VARIABLE '=' E ';'    { setvalue($1, $3); printf("Value %d assigned to %c\n", $3, $1); }
  53.             ;
  54.  
  55. E :
  56.               E '+' T       { $$ = $1 + $3; }
  57.             | T
  58.             ;
  59. T :
  60.               T '*' F       { $$ = $1 * $3; }
  61.             | F
  62.             ;
  63. F :          
  64.               INTEGER
  65.             | VARIABLE      { $$ = getvalue($1); }
  66.             | '(' E ')'     { $$ = $2; }
  67.             ;
  68. %%
  69.  
  70. void yyerror(char *fmt, ...) {
  71.     va_list args;
  72.  
  73.     fprintf(stderr, "Error: ");
  74.  
  75.     va_start(args, fmt);
  76.     vfprintf(stderr, fmt, args);
  77.     va_end(args);
  78.  
  79.     fprintf(stderr, "\n");
  80. }
  81.  
  82. int getidx(char var) {
  83.     if(var >= 'a' && var <= 'z')
  84.         return var - 'a';
  85.     else if(var >= 'A' && var <= 'Z')
  86.         return var - 'A' + 26;
  87.     return -1;
  88. }
  89.  
  90. void setvalue(char var, int val) {
  91.     int idx = getidx(var);
  92.     if(idx == -1) {
  93.         exit(EXIT_FAILURE);
  94.     }
  95.     declared[idx] = 1;
  96.     symbol_table[idx] = val;
  97. }
  98.  
  99. int getvalue(char var) {
  100.     int idx = getidx(var);
  101.     if(idx == -1) {
  102.         exit(EXIT_FAILURE);
  103.     }
  104.     if(!declared[idx]) {
  105.         yyerror("variable %c not declared", var);
  106.         exit(EXIT_FAILURE);
  107.     }
  108.     return symbol_table[idx];
  109. }
  110.  
  111. int main()
  112. {
  113.     yyparse();
  114.     return 0;
  115. }
  116.  
  117.  
Advertisement
Comments
  • madvice
    122 days
    # text 0.09 KB | 0 0
    1. Blog at: https://syedumair327.blogspot.com/2025/10/building-compiler-part-2-of-4-getting.html
Add Comment
Please, Sign In to add comment