I have been trying to write a Lex program to convert octal numbers to decimal numbers. But after accepting the number as input, but not doing anything. No output. And no program termination. The program is still running without any output.

What might be the error? I have a strong feeling about the `yywrap`

and `todec`

functions doing mischief.

Thanks in advance. :)

Source code :

```
%{
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int v = 0, o = 0, d = 0; //valid bit, octal number, decimal number
%}
%%
^[0-7]+ {v = 1; o = atoi(yytext);}
[\n] {;}
. {v = 0;}
%%
int yywrap()
{
if (v)
{
d = todec(o);
printf("Decimal is %d\n", d);
}
else
{
printf("Invalid");
}
}
int todec(int oct)
{
int dec = 0, pos = 0;
while(oct)
{
int a = oct % 10;
dec += a * pow(8, pos);
pos++;
oct /= 10;
}
return dec;
}
int main()
{
printf("Enter the octal number: ");
yylex();
return 0;
}
```

I'll quote http://dinosaur.compilertools.net/flex/flex_10.html

**When the scanner receives an end-of-file indication from YYINPUT, it then checks the yywrap() function*

So, if you simulate EOL behaviour using ctrl-d (after entering the number and pressing the return key), you can see that your calculations are correct.

If you want to see the results immediately, you will have to write the output after you compute it.

Cheers

Similar Questions

I am currently working on a basic program which converts a binary number to an octal. Its task is to print a table with all the numbers between 0-256, with their binary, octal and hexadecimal equivale

This question already has an answer here: In what situations is octal base used? 10 answers I'm a college programming student with two years of programming experience in Java and Python. We've

I read the JSON from a web service and converted all the JSON objects to HashMap<String, String> using GSON. I tested and my JSONObject it's fine (no decimal point in any of the numbers), but t

I have a string such as 3.1 ml or abc 3.1 xywazw I'd like to extract 3.1 from this string. I have found many questions on stackoverflow about the extraction of numbers from a character string, b

Im very new html, javascript, and css so please forgive if my question sounds idiotic to you. My question is how can I prevent the function toFixed() from rounding of the decimal number. Here's my lin

This question already has an answer here: Integer ASCII value to character in BASH using printf 5 answers I wanted to convert the numbers from 1 to 26 to Latin alpgabet {a-z}. 1 correspnds to

How can I convert the numbers in the range 1 through 26 to their respective letter position in the alphabet? 1 = A 2 = B ... 26 = Z

In Perl, how do I convert multiple decimal numbers in a line of a file to their hexadecimal equivalents? I have code which will convert the last number found but leaves the prior numbers unconverted,

I am attempting to manually convert numbers between decimal and hexadecimal. I have it working for positive numbers and converting a negative decimal to 'negative' hexadecimal but I can't convert it f

I need to set a jTextField to accept only numbers and one decimal point.Nothing else. Decimal point can't be typed more than once and no other characters are allowed. How can I do this? I typed this c

I've been trying for a while to find a regular expression that I need. 3 characters before . and two characters after the decimal point. I try this [+ -]?[0-9]{0,3}[.]?[0-9]{0,2} # but accepted as 55

I have a problem with decimal numbers. If I use .(dot) instead of ,(comma) in the textbox it comes null in controller. I know its a language issue because in spanish we use comma instead of dot for de

Trying to and from bases in Ruby... the code I've laid out seems quite repetitious. Is ther a better way? module Converter def self.convert(value, from, to) case from when :hex case to when :dec # cod

I wrote this program to convert decimal numbers to another bases but when I run it with eclipse the answer is 0 for any number. import java.util.Scanner; public class dtb { public static void main(Str

I would like to generate uniformly distributed random numbers between 0 and 0.5, but truncated to 2 decimal places. without the truncation, I know this is done by import numpy as np rs = np.random.Ran

I have following data table in MS Excel phone no cost 1231231221 10.45 1234234232 12.67 1234234453 30.00 The code below is printing phone no correctly but leaving out the decimal places of cost. for

I'm probably completely wrong, and I don't really know anything about it, but I have a question about decimal number data types in programming languages. I understand that floats aren't completely pre

I have a form and I am using jquery validation plugin to validate it. Now I am trying to validate decimal number input. I have tried the following code, but its not working. Is the problem with the re

I have a text field that needs to remain only text or decimal. Here is the code that I'm currently using to replace everything except numbers and a decimal point. Issue is, I can't figure out a regex

how to restrict user to enter only numbers with 6 digits and two decimal values in textbox using javascript?? //Function to allow only numbers to textbox function validate(key) { //getting key code of

I have to put the price of some items inside a mysql table. When creating the table I'm using DECIMAL(10,2) as I don't need more than 2 digits after the comma (for example: 123,45 would be accepted as

I'm curious as to if it'd be possible to convert a very, very large decimal number such as 1.67119535743*10^33/1.67119535743E+33 to hexadecimal via PHP or C#. All my previous attempts have failed, unf

How to convert a list of numbers to a refromatted string. I am new to the list and have seen simple Python type programming that can be executed in a simple text editor. I have a List of numbers and w

what is a recommended way to perform addition and subtraction operations on Octal numbers? (My search results to date have yielded only conversions of octals, but not how to perform arithmetic operati

I am trying to truncate decimal numbers to decimal places. Something like this: 5.467 -> 5.46 985.943 -> 985.94 toFixed(2) does just about the right thing but it rounds off the value. I don't n

Example: How to convert hexadecimal 8F to decimal?

Hi I'm new at programming and Im trying to learn Python. Im trying to make a program that converts numbers into the word for the number. Ex 15 would be fifteen. In the moment Im making a small func

I'm new to C. I was writing a program to print all the decimal numbers from 0 to a given number(as provided by the user) but my output is not what I had expected. I don't understand what mistake is th

I'm trying to design a script that given any arbitrary command-line input in euros and cents, calculate the change, using the minimum number of notes or coins. The script works fine with numbers with

So I need to change doubles such as 143.203030 to 143.20 rounding the second digit up or down(like money) my plan was to turn the double into an array and then if the third number after the decimal is

I am a beginner in programmin in general and R specifically. I would like to generate a set of random numbers in a normal distribution but to limit the decimal places in these numbers to only 2. I hav

After appending a list with one column(with string header and decimal numbers) from a CSV file, the numbers become single quoted in the list. Cant do a sum etc. I popped the header after the list was

Can I use decimal numbers (18.9 or 75.5) for padding, margin or other styles? all browsers (such as IE5, IE6) support it? Thanks.

Within django models I have created a decimal field like this: price = models.DecimalField(_(u'Price'), decimal_places=2, max_digits=12) Obviously it makes no sense for the price to be negative or ze

I am trying to find a clean way of referencing an array's index using octal numbering. If I am looking for the array index that is octal 13 it should return the value for a[11]. This is what I have c

Assume one has an vector or array of N elements (N can be very large) containing the octal representation of a non negative integer. How do I get the decimal representation of the number from this arr

In my Laravel 4 app, I have a form on which I need to validate numbers with comma as decimal separator. For the moment, it only work with point because my validation rule is : public static $rules = a

Can any one give me some predefined methods or user defined methods to convert string numbers(example : 123455) to comma separated integer value (example : 1,23,455). Thanks in advance, sathish

Why do some numbers lose accuracy when stored as floating point numbers? For example, the decimal number 9.2 can be expressed exactly as a ratio of two decimal integers (92/10), both of which can be e

I am new to R and I have problems with converting a list into a numeric object. I have a list of numbers like this 0,4484 0,4495 0,4516 0,4526 0,4533 0,4571 0,4592 0,4625 0,4654 0,4671 0,4686 0,4697

float f = 0.00f; System.out.println(f); gives the output: 0.00 I'd like to format a number represented by a percentage to 2 decimal places. But the result should be a float and not a string. e.g.

I have a number that represents a software version (ex: 1.2.0.14) and I need to separate each number that is divided by a decimal and store each number as a separate variable. Example: Original number

How do you truncate all numbers after a decimal in Python 3? For example truncate 3.444 to be just 3.

I need to use an accurate number that has many decimal places. If I use the type decimal (because float is not precise enough to store such numbers) I can have something like 0.003352466 and it is pre

Whenever I insert a value like: 1 into a decimal field it will immediately rejects it. Is there any code in PHP to convert a value of say 1 to 01.00 or 1.00

In PHP textfield: Prevent the user from entering more then 3 numbers before decimal point for example: if user enter 123.12 its acceptable, if user enter 12.12 its also acceptable, but if user enter 1

I am looking for a regular expression which will convert any amount value like 123/123.34/12345.99 will convert to a comma separeted value like 123/123.34/12,345.99. I am going to use this regular exp

I have been looking for an elegant way in php to convert an IP address into its octal representation without having to do each of the four digits separately. for example, the IP: 1.2.3.4 would be 000

I need a function to compare two numbers so that 10.12432 = 10.124 and 10.12432 != 10.123. That is the more accurate number needs to be rounded according to the decimal places of the less accurate one

This question already has an answer here: Is floating point math broken? 14 answers So I am making a script that adds two numbers (decimal numbers) together, which I have encountered a problem.