fork download
  1. %{
  2. #include<stdio.h>
  3. #include<string.h>
  4.  
  5. int value(char ch)
  6. {
  7. switch(ch)
  8. {
  9. case 'I': return 1;
  10. case 'V': return 5;
  11. case 'X': return 10;
  12. case 'L': return 50;
  13. case 'C': return 100;
  14. case 'D': return 500;
  15. case 'M': return 1000;
  16. default: return 0;
  17. }
  18. }
  19.  
  20. int romanToDecimal(char roman[])
  21. {
  22. int i, sum = 0;
  23. int len = strlen(roman);
  24.  
  25. for(i = 0; i < len; i++)
  26. {
  27. if(i < len - 1 && value(roman[i]) < value(roman[i + 1]))
  28. sum -= value(roman[i]);
  29. else
  30. sum += value(roman[i]);
  31. }
  32.  
  33. return sum;
  34. }
  35. %}
  36.  
  37. %%
  38.  
  39. [IVXLCDM]+
  40. {
  41. printf("Roman Numeral : %s\n", yytext);
  42. printf("Decimal Value : %d\n", romanToDecimal(yytext));
  43. }
  44.  
  45. \n ;
  46.  
  47. . {
  48. printf("Invalid Roman Numeral\n");
  49. }
  50.  
  51. %%
  52.  
  53. int yywrap()
  54. {
  55. return 1;
  56. }
  57.  
  58. int main()
  59. {
  60. printf("Enter Roman Numeral: ");
  61. yylex();
  62. return 0;
  63. }
  64.  
Success #stdin #stdout #stderr 0.03s 6844KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
ERROR: /home/WYIBIm/prog:2:1: Syntax error: Operator expected
ERROR: /home/WYIBIm/prog:63:1: Syntax error: Unexpected end of file
ERROR: '$runtoplevel'/0: Undefined procedure: program/0
   Exception: (3) program ? EOF: exit