Syntax-directed translation in YACC ---------------------------------------------------------------------- Exercise 4. Write a translator that accepts single logical expressions being bulit of: constants 'true' and 'false', variable names (sequences of small letters), connectives: 'and', or', 'not' and parentheses. Every correct expression sholuld be transcribed to the output with the names of connectives set to uppercase. For example, the input: a or b and (not cd or efg) or false should be transcribed as: a OR b AND ( NOT cd OR efg ) OR false The scanner is as follows: %{ # include #include "ytab.h" %} %% "true" {return( true);} "false" {return( false);} "and" {return( and);} "or" {return( or);} "not" {return( not);} [a-z][a-z]* { yylval.text=strdup(yytext); return(nazwa); } "(" { return('('); } ")" { return(')'); } \ |\n ; . {{yyterminate();} ------------------------------------------------------------------------------ Exercise 5. Use the YACC generator to create a program to translate C-like 'for' statements to Pascal-like ones. The simplified syntax scheme of c 'for' statements is as follows; for ( 'ident' ='const1' ; 'ident' 'relop' 'const2'; 'ident' 'incdec') {} where 'ident' is the name of a variable, 'const1', 'const2' are integer constants, 'relop' is one of : '<' '<=', '>', '>=', 'incdec' stand for increment or decrement operator. For example: for (x=0;x<5;x++) {} is translated to: for x:=0 to 4 do ; while for the input: for (y=13;y<=0;y--) {} the output should be as follows: for y:=13 downto 0 do ; Note, that the translator should only check the context-free syntax of the input sentence! The scanner is given below: %{ #include #include "ytab.h" %} %% "for" {return( forr);} [0-9]+ { yylval.ival=atoi(yytext); return( number); } "<=" {return( le);} ">=" {return( ge);} "=" {return ('=');} "++" {return (incr);} "--" {return(decr);} "<" {return( '<');} ">" { return('>'); } [a-z][a-z]* { yylval.text=strdup(yytext); return(nazwa);} ";" { return(';'); } ")" { return(')'); } "{" {return ('{');} "}" {return('}');} "(" { return('('); } \ |\n ; . {yyterminate();}