Bison C++ - subtracting












1















I'm making a simple calc using lex and bison. What should it do is to parse every mentioned subtracting - 1 - -1, 1- 1, 1--1, and what's the most important: 1-1. The first three cases are working, but in the last one it looks as if it separated the sentence into numbers 1 and -1 without a sign between them and that's why there is an error.
I read about precedence and how to use it, nothing has worked.



Below is my shortened code, ready to copy:



.l file



%{
#include "y.tab.h"

void yyerror (const char* s);
int yylex();
%}

%%
[-+*/%^()n] { return yytext[0]; }
[0] {yylval = 0; return number;}
[-]?[1-9][0-9]* {yylval = atoi(yytext); return number;}
%%

int yywrap(void) {return 1;}
void yyerror (const char* s) {;}


.y file



%{
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <cmath>

void yyerror (const char*);
int yylex();
%}

%token number


%right NEG
%left '-'

%%
program:
| line program
;

line: 'n'
| expression 'n' { std::cout << "Score: " << $1 << "n"; }
;

expression: number { ; }
| expression '-' expression { $$ = $1-$3; }
| '-' expression %prec NEG { $$ = -$2; }
;
%%

int main (void) {
return yyparse();
}


input and output:



1- - 1
Score: 2
1--1
Score: 2
1-1
<here is an error>









share|improve this question




















  • 1





    The problem is that 1-1 tokenizes to two consecutive numbers, values 1 and -1, and your expression rules don't allow two consecutive numbers. In C, there are no negative integer constants. Maybe you should decide that you don't recognize them either.

    – Jonathan Leffler
    Nov 14 '18 at 19:16
















1















I'm making a simple calc using lex and bison. What should it do is to parse every mentioned subtracting - 1 - -1, 1- 1, 1--1, and what's the most important: 1-1. The first three cases are working, but in the last one it looks as if it separated the sentence into numbers 1 and -1 without a sign between them and that's why there is an error.
I read about precedence and how to use it, nothing has worked.



Below is my shortened code, ready to copy:



.l file



%{
#include "y.tab.h"

void yyerror (const char* s);
int yylex();
%}

%%
[-+*/%^()n] { return yytext[0]; }
[0] {yylval = 0; return number;}
[-]?[1-9][0-9]* {yylval = atoi(yytext); return number;}
%%

int yywrap(void) {return 1;}
void yyerror (const char* s) {;}


.y file



%{
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <cmath>

void yyerror (const char*);
int yylex();
%}

%token number


%right NEG
%left '-'

%%
program:
| line program
;

line: 'n'
| expression 'n' { std::cout << "Score: " << $1 << "n"; }
;

expression: number { ; }
| expression '-' expression { $$ = $1-$3; }
| '-' expression %prec NEG { $$ = -$2; }
;
%%

int main (void) {
return yyparse();
}


input and output:



1- - 1
Score: 2
1--1
Score: 2
1-1
<here is an error>









share|improve this question




















  • 1





    The problem is that 1-1 tokenizes to two consecutive numbers, values 1 and -1, and your expression rules don't allow two consecutive numbers. In C, there are no negative integer constants. Maybe you should decide that you don't recognize them either.

    – Jonathan Leffler
    Nov 14 '18 at 19:16














1












1








1








I'm making a simple calc using lex and bison. What should it do is to parse every mentioned subtracting - 1 - -1, 1- 1, 1--1, and what's the most important: 1-1. The first three cases are working, but in the last one it looks as if it separated the sentence into numbers 1 and -1 without a sign between them and that's why there is an error.
I read about precedence and how to use it, nothing has worked.



Below is my shortened code, ready to copy:



.l file



%{
#include "y.tab.h"

void yyerror (const char* s);
int yylex();
%}

%%
[-+*/%^()n] { return yytext[0]; }
[0] {yylval = 0; return number;}
[-]?[1-9][0-9]* {yylval = atoi(yytext); return number;}
%%

int yywrap(void) {return 1;}
void yyerror (const char* s) {;}


.y file



%{
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <cmath>

void yyerror (const char*);
int yylex();
%}

%token number


%right NEG
%left '-'

%%
program:
| line program
;

line: 'n'
| expression 'n' { std::cout << "Score: " << $1 << "n"; }
;

expression: number { ; }
| expression '-' expression { $$ = $1-$3; }
| '-' expression %prec NEG { $$ = -$2; }
;
%%

int main (void) {
return yyparse();
}


input and output:



1- - 1
Score: 2
1--1
Score: 2
1-1
<here is an error>









share|improve this question
















I'm making a simple calc using lex and bison. What should it do is to parse every mentioned subtracting - 1 - -1, 1- 1, 1--1, and what's the most important: 1-1. The first three cases are working, but in the last one it looks as if it separated the sentence into numbers 1 and -1 without a sign between them and that's why there is an error.
I read about precedence and how to use it, nothing has worked.



Below is my shortened code, ready to copy:



.l file



%{
#include "y.tab.h"

void yyerror (const char* s);
int yylex();
%}

%%
[-+*/%^()n] { return yytext[0]; }
[0] {yylval = 0; return number;}
[-]?[1-9][0-9]* {yylval = atoi(yytext); return number;}
%%

int yywrap(void) {return 1;}
void yyerror (const char* s) {;}


.y file



%{
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
#include <cmath>

void yyerror (const char*);
int yylex();
%}

%token number


%right NEG
%left '-'

%%
program:
| line program
;

line: 'n'
| expression 'n' { std::cout << "Score: " << $1 << "n"; }
;

expression: number { ; }
| expression '-' expression { $$ = $1-$3; }
| '-' expression %prec NEG { $$ = -$2; }
;
%%

int main (void) {
return yyparse();
}


input and output:



1- - 1
Score: 2
1--1
Score: 2
1-1
<here is an error>






c++ bison






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 19:13









Jonathan Leffler

568k916801031




568k916801031










asked Nov 14 '18 at 19:02









J.G.J.G.

434




434








  • 1





    The problem is that 1-1 tokenizes to two consecutive numbers, values 1 and -1, and your expression rules don't allow two consecutive numbers. In C, there are no negative integer constants. Maybe you should decide that you don't recognize them either.

    – Jonathan Leffler
    Nov 14 '18 at 19:16














  • 1





    The problem is that 1-1 tokenizes to two consecutive numbers, values 1 and -1, and your expression rules don't allow two consecutive numbers. In C, there are no negative integer constants. Maybe you should decide that you don't recognize them either.

    – Jonathan Leffler
    Nov 14 '18 at 19:16








1




1





The problem is that 1-1 tokenizes to two consecutive numbers, values 1 and -1, and your expression rules don't allow two consecutive numbers. In C, there are no negative integer constants. Maybe you should decide that you don't recognize them either.

– Jonathan Leffler
Nov 14 '18 at 19:16





The problem is that 1-1 tokenizes to two consecutive numbers, values 1 and -1, and your expression rules don't allow two consecutive numbers. In C, there are no negative integer constants. Maybe you should decide that you don't recognize them either.

– Jonathan Leffler
Nov 14 '18 at 19:16












1 Answer
1






active

oldest

votes


















3














Your precedence annotation didn't change anything for you because the rule '-' expression isn't actually used in 1-1. The problem with that input is that the parser only sees two integer tokens and nothing you can do in the parser can change that.



Instead you'll need to make the lexer produce three tokens for that input instead of two. You can do by simply removing the [-]? from your rule for number tokens. With that change 1-1 will be tokenized as number, '-', number and your parser will work.



Note that this will still allow negative numbers because your '-' expression rule handles that - it just won't treat negative numbers as a single token, which is fine.






share|improve this answer
























  • It will also treat - 1 with a space as -1 — which shouldn't matter much.

    – Jonathan Leffler
    Nov 14 '18 at 19:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307097%2fbison-c-subtracting%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Your precedence annotation didn't change anything for you because the rule '-' expression isn't actually used in 1-1. The problem with that input is that the parser only sees two integer tokens and nothing you can do in the parser can change that.



Instead you'll need to make the lexer produce three tokens for that input instead of two. You can do by simply removing the [-]? from your rule for number tokens. With that change 1-1 will be tokenized as number, '-', number and your parser will work.



Note that this will still allow negative numbers because your '-' expression rule handles that - it just won't treat negative numbers as a single token, which is fine.






share|improve this answer
























  • It will also treat - 1 with a space as -1 — which shouldn't matter much.

    – Jonathan Leffler
    Nov 14 '18 at 19:17
















3














Your precedence annotation didn't change anything for you because the rule '-' expression isn't actually used in 1-1. The problem with that input is that the parser only sees two integer tokens and nothing you can do in the parser can change that.



Instead you'll need to make the lexer produce three tokens for that input instead of two. You can do by simply removing the [-]? from your rule for number tokens. With that change 1-1 will be tokenized as number, '-', number and your parser will work.



Note that this will still allow negative numbers because your '-' expression rule handles that - it just won't treat negative numbers as a single token, which is fine.






share|improve this answer
























  • It will also treat - 1 with a space as -1 — which shouldn't matter much.

    – Jonathan Leffler
    Nov 14 '18 at 19:17














3












3








3







Your precedence annotation didn't change anything for you because the rule '-' expression isn't actually used in 1-1. The problem with that input is that the parser only sees two integer tokens and nothing you can do in the parser can change that.



Instead you'll need to make the lexer produce three tokens for that input instead of two. You can do by simply removing the [-]? from your rule for number tokens. With that change 1-1 will be tokenized as number, '-', number and your parser will work.



Note that this will still allow negative numbers because your '-' expression rule handles that - it just won't treat negative numbers as a single token, which is fine.






share|improve this answer













Your precedence annotation didn't change anything for you because the rule '-' expression isn't actually used in 1-1. The problem with that input is that the parser only sees two integer tokens and nothing you can do in the parser can change that.



Instead you'll need to make the lexer produce three tokens for that input instead of two. You can do by simply removing the [-]? from your rule for number tokens. With that change 1-1 will be tokenized as number, '-', number and your parser will work.



Note that this will still allow negative numbers because your '-' expression rule handles that - it just won't treat negative numbers as a single token, which is fine.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 19:16









sepp2ksepp2k

296k38596613




296k38596613













  • It will also treat - 1 with a space as -1 — which shouldn't matter much.

    – Jonathan Leffler
    Nov 14 '18 at 19:17



















  • It will also treat - 1 with a space as -1 — which shouldn't matter much.

    – Jonathan Leffler
    Nov 14 '18 at 19:17

















It will also treat - 1 with a space as -1 — which shouldn't matter much.

– Jonathan Leffler
Nov 14 '18 at 19:17





It will also treat - 1 with a space as -1 — which shouldn't matter much.

– Jonathan Leffler
Nov 14 '18 at 19:17




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53307097%2fbison-c-subtracting%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma