0

I can use this very nicely but in the expr i want to validate the expr tADD etc i created, but i can not access the expr inside operation, how do i access it ? it just returns the result that is 0

%{
#include <stdio.h>
void yyerror (const char *s)
#define YYSTYPE char *
extern YYSTYPE yylval;
extern int line; 
{}
%}

%token tADD tSUB tMUL tDIV tSTRING tNUM tPRINT tGET tSET tFUNCTION tRETURN tIDENT tEQUALITY tIF tGT tLT tGEQ tLEQ tINC tDEC

%start prog


%%
prog:       '[' stmtlst ']' 
;

stmtlst:    stmtlst stmt |
;

stmt:       setStmt | if | print | unaryOperation | expr | returnStmt
;

getExpr:    '[' tGET ',' tIDENT ',' '[' exprList ']' ']'
        | '[' tGET ',' tIDENT ',' '[' ']' ']'
        | '[' tGET ',' tIDENT ']'
;

setStmt:    '[' tSET ',' tIDENT ',' expr ']'
;

if:     '[' tIF ',' condition ',' '[' stmtlst ']' ']'
        | '[' tIF ',' condition ',' '[' stmtlst ']' '[' stmtlst ']' ']'
;

print:      '[' tPRINT ',' '[' expr ']' ']'
;

operation:  '[' tADD ',' expr ',' expr ']' {
                    if($2){
                        printf("op\n");
                    }
                    printf("%d here ", $4);
            }
        | '[' tSUB ',' expr ',' expr ']'
        | '[' tMUL ',' expr ',' expr ']'
        | '[' tDIV ',' expr ',' expr ']'
;   

unaryOperation: '[' tINC ',' tIDENT ']'
        | '[' tDEC ',' tIDENT ']'
;

expr:        tNUM | tSTRING | function | operation | condition
            
;

function:    '[' tFUNCTION ',' '[' parametersList ']' ',' '[' stmtlst ']' ']'
        | '[' tFUNCTION ',' '[' ']' ',' '[' stmtlst ']' ']'
;

condition:  '[' tEQUALITY ',' expr ',' expr ']'
        | '[' tGT ',' expr ',' expr ']'
        | '[' tLT ',' expr ',' expr ']'
        | '[' tGEQ ',' expr ',' expr ']'
        | '[' tLEQ ',' expr ',' expr ']'
;

returnStmt: '[' tRETURN ',' expr ']'
        | '[' tRETURN ']'
;

parametersList: parametersList ',' tIDENT | tIDENT
;

exprList:   exprList ',' expr | expr{
                printf("%s\n", $1);
        }
;

%%
int main ()
{
if (yyparse()) {
// parse error
printf("ERROR\n");
return 1;
}
else {
// successful parsing
printf("OK\n");
return 0;
}
}

I use $4 but I always get 0 as the value for all Operations,

expr:        tNUM | tSTRING | function | operation | condition
            
;

Also flex that i use

%{
#include "username-hw3.tab.h" 
%}
%%

['][^']*[']     return tSTRING;

["]Get["]       return tGET;
["]Set["]       return tSET;
["]Function["]      return tFUNCTION;
["]Print["]     return tPRINT;
["]If["]        return tIF;
["]Return["]        return tRETURN;

["]"+"["]               return tADD;
["]"-"["]               return tSUB;
["]"*"["]               return tMUL;
["]"/"["]               return tDIV;
["]"++"["]      return tINC;
["]">"["]       return tGT;
["]"=="["]      return tEQUALITY;
["]"--"["]      return tDEC;
["]"<"["]       return tLT;
["]"<="["]      return tLEQ;
["]">="["]      return tGEQ;


["][_a-zA-Z][a-zA-Z0-9_]*["]    return tIDENT;
                
[0-9]+ |
-[0-9]+ |       
[0-9]+"."[0-9]+ |
-[0-9]+"."[0-9]+    return tNUM; 
    
[ \t\n]+ 
.           return yytext[0];

%%
Ahmed Can Unbay
  • 2,694
  • 2
  • 16
  • 33
  • `$4 + $6` is the sum of the value of symbol 4 (the first `expr`) and symbol 6 (the second one). – rici Apr 16 '21 at 03:13
  • 1
    The bison manual includes [some simple examples](https://www.gnu.org/software/bison/manual/bison.html#Examples) which will help you understand the basic concepts. – rici Apr 16 '21 at 03:19
  • i use `printf("%d here ", $4);` i always get `0` for some reason... Maybe something wrong, @rici – Ahmed Can Unbay Apr 16 '21 at 03:22
  • how do i get the type of the constant ? maybe int or, i have them all defined actually – Ahmed Can Unbay Apr 16 '21 at 03:27
  • Inside the rule for `expr`, there is only `$1`, the value of operation. In the rules for `operation`, the implicit rule is `{ $$ = $0; }`, IIRC, so you get the value associated with `'['` 'returned'. Your grammar is not complete: the `%start` symbol is `prog` but `prog` is not defined, and the tokens `tAdd`, `tSUB`, `tMUL` and `tDIV` are not defined either. Please read about how to create an MCVE ([Minimal, Complete, Verifiable Example](https://stackoverflow.com/help/mcve) — or MRE or whatever name SO now uses) or an SSCCE ([Short, Self-Contained, Correct Example](http://sscce.org/)). – Jonathan Leffler Apr 16 '21 at 03:28
  • yes my `expr` is defined as above, how should i change it so that i can get values? i need to make typechecks for mismatch or do the op itself @JonathanLeffler – Ahmed Can Unbay Apr 16 '21 at 03:30
  • Please see the updated version @JonathanLeffler – Ahmed Can Unbay Apr 16 '21 at 03:35
  • I am just worried this could be about windows version of bison but i think it might just be something else, it is able to retrieve the operations but yeah the values are just the implicite rule, i cant get the constant expressions – Ahmed Can Unbay Apr 16 '21 at 03:47
  • @turmuka: it's not about windows. It's because you haven't bothered to learn the first thing about flex and bison. I know that reading technical docs in a second or third language is a pain, because that's where I live. But there is no good substitute and SO cannot offer a complete "how thus technology works" course. The bison manual is a pretty good start, though. Or John Levine's book, published by Safari. – rici Apr 16 '21 at 04:36
  • @JonathanLeffler: the implicit rule is `$$ = $1`. `$0` should not normally be used, although it definitely has its purpose. – rici Apr 16 '21 at 04:39
  • @turmuka, Anyway, you need to set the value of `yylval` in your flex rule. If you don't, bison cannot guess what it might be. (And if you want values to be floating point, you do need to declare a semantic value type because the default is `int`. That's described in the manual chapter after the examples, although some examples also show how to do it.) – rici Apr 16 '21 at 04:43
  • @rici — thanks; I wasn't thinking straight enough. – Jonathan Leffler Apr 16 '21 at 04:57
  • `#define YYSTYPE char *` ` extern YYSTYPE yylval;` I added these to my .y file, I'm new to this so the questions i am suffering from might be low to you, i appreciate any kind of help, you are getting me further. it doesnt seem to be enough, im doing it wrong @JonathanLeffler @rici – Ahmed Can Unbay Apr 16 '21 at 11:43
  • i set `%union ` but how do i access it, last bit of help – Ahmed Can Unbay Apr 16 '21 at 13:02

0 Answers0