user1010101
user1010101

Reputation: 1658

Simple Flex Program

Given a string -> The # 1 Temprature is 32CELSIUS

I Should print output -> The # 1 Temprature is 64FAHRENHEIT

I know it is not the correct conversion but that is not the point i am just suppose to double the value in front of celsius and return it.

#include<stdio.h>
%}
digit    [0-9]
celsius   "CELSIUS"
%%

{digit}+{celsius}        {printf("FAHRENHEIT");}


%%

int main(void)
{
  yylex();
  return 0;
}

This is the code i have so far i am kind of lost on how to convert the number since i am not suppose to convert any number but only the one that is front of the world Celsius. Can anyone guide me

Upvotes: 1

Views: 490

Answers (1)

plafratt
plafratt

Reputation: 812

The code you posted works for me as expected. All you need to do is add a call to sscanf() (right before the call to printf()) with yytext as the first argument. This will allow you to store the integer in a variable, which you can then double and print back out via your printf() call.

Change:

{digit}+{celsius}        {printf("FAHRENHEIT");}

To:

{digit}+{celsius}        { int x; sscanf(yytext,"%d",&x); printf("%dFAHRENHEIT",x*2);}

If you want to match a float instead of an int, change your match rule to the following:

{digit}+.{digit}+{celsius}

Upvotes: 2

Related Questions