logoISU  

CS 456 - Systems Programming

Spring 2024

Displaying ./code/apr11/example5.y

/*

Compile with :
> lex example5.l
> yacc -d example5.y
> gcc lex.yy.c y.tab.c -o example5

*/

%{
#include <stdio.h>
#include <string.h>

void yyerror(const char *str)
{
	fprintf(stderr,"error: %s\n",str);
}

int yywrap()
{
	return 1;
}

main()
{
	yyparse();
}

%}

%token NUMBER TOKHEAT STATE TOKTARGET TOKTEMPERATURE

%%

commands:
	| commands command
	;


command:
	heat_switch
	|
	target_set
	;

heat_switch:
	TOKHEAT STATE 
	{
		if($2)
			printf("\tHeat turned on\n");
		else
			printf("\tHeat turned off\n");
	}
	;

target_set:
	TOKTARGET TOKTEMPERATURE NUMBER
	{
		printf("\tTemperature set to %d\n",$3);
	}
	;