c - Get char in the middle of the string YACC -
my question kinda simple, suppose. here code,
%{ #include <stdio.h> #include <ctype.h> #include <stdlib.h> int yylex(void); void yyerror(char const *c); int yywarp(){ return 1; } %} %token letter mid %% input: {printf("enter polindrom:\n");} | input line; line: anal '\n' {printf("good\n\n");} | error '\n' {yyerrok;} ; anal: mid mid { printf("%d %d\n", $1, $2); if($1!=$2) yyerror; } |letter anal letter { printf("%d %d\n", $1, $3); if($1!=$3) yyerror; } ; %% int yylex(void) { int c; char a[10]; c = getchar(); if(c == 'a'){ yylval = c; return mid; } if(isalpha(c)){ yylval = c; return letter; } if(c == eof) return 0; return c; } void yyerror(char const *c) { fprintf(stderr, "%s\n", c); } int main(){ return(yyparse()); }
as can see, mid returns when input 'a'. question is: how real middle element? task recognize palindromes using yacc. think teacher won't accept if use "for" cycle it.
the language of palindromes not lr(1), yacc
cannot generate parser without oracle lets know middle of string is. example includes such oracle; easy if middle letter (or pair of letters) have distinct value. if part of problem specification, on right track.
bison
parse palindromes if ask generate glr
parser instead of lalr(1)
parser. fact yacc
rather bison
suggests not appropriate solution, , not in general appropriate solution because glr parsers not linear-time grammars this, whereas obvious linear-time parsing solution exists.
if else fails, parse input list of characters, , check see if palindrome in final reduction action.
Comments
Post a Comment