explaining context free grammar ambiguity for job interview - theory

I was at a job interview, and this is the question they asked me,
Are these two below ambigous? If they are, provide a string. If they are not, prove why they are not.
I couldn't solve it, and would like to know the answer and the reason for the future.
Question 1
S-->XaaaX
X-->aX | bX | e(epsilon)
Question 2
S-->aaS | aaaS | a
Again, this is not HW.
Thank you. An explanation would help.

We recall that a grammar is ambiguous if (and only if) some production from the grammar has more than one possible derivation.
In question 1 the symbol S expands to XaaaX and then the available alternatives for expanding the symbol X include aX and epsilon (ε). Conventionally the symbol epsilon represents an empty string. Expanding X as epsilon in aX produces a. So there are at least two ways to get aaaa. Richard Mckenna, I'll leave it to you to find them.
In question 2 the symbol S expands to aaS, aaaS, or a. There are at least two ways to get aaaaaa. Again I'll leave it to you to find the derivations.
If you wish, you may write your derivations on this page.

Related

C++/CLI Load variables from a .txt file using a special structure

Rather long question, and quite long story so I'll try to be as quick and precise as I can. I've written a program which allows the user to create questions for a quiz, and those options be exported into a .txt file with the below structure:
#
Level: 1
Ref: testRef
Question: test question
A: test A
B: test B
C: test C
D: test D
Ans: A
APerc: 100
BPerc: 0
CPerc: 0
DPerc: 0
Phone Answer: Right, I know this. The answer is 100% A. Good luck!
50/50: B
50/50 Percentage 1: 100
50/50 Percentage 2: 0
Force: True
!
Where
# indicates the start of the question,
Level is the part of the quiz this question is worth (the higher it is, the more difficult it is),
A, B, C and D are the alternative answers,
ans is the correct answers,
APerc to 50/50 Percentage 2 are all info for assists to the user to answer the question if they use them,
the value of force is whether this question should appear definitively or not, since their may be more than once question with the same level
and ! indicates the end of a question.
Now, in terms of code what would be the easiest way to read the whole file and identify each of these individual questions with the aforementioned delimiters?
Would it be good to use something like StreamReader? I'd rather read the whole file, then identify if any questions have been forced, then randomly pick 15 questions, either at the start of runtime or during the reveal of the next question, with different levels, possibly passing the value of the current level to the read function and then store all the question info in variables.

Implement the Unix 'dc' utility in C [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am just hoping on getting some tips on how to get started on my assignment.
It reads as follows:
"Implement a program that behaves similarly to the 'dc' utility, which evaluates expressions in a postfix notation, and also supports additional computations using variables and macro strings. Feel free to experiment with the existing utility and consulting its manual pages.
The assignment will support only a subset of the complete utility:
-- all numeric values will be integers, only using radix 10
-- numbers may be assumed to be within the range of 32-bit signed numbers
-- all register names will be alphanumeric (a subset of those dc allows)
-- no command line options or arguments will be required
-- only the following commands are are required (listed in the same order as the manual)
p n f + - * / % ^ c d r s l S L x > !> < !< = != q #
Some key differences between the assignment and the existing utility:
-- all input will be through standard input only (but not necessarily keyboard)
-- register stacks are initially not empty, but filled with infinite zeros
-- the q command will exit the program, regardless of macro call nesting level
-- additional spaces may appear between input tokens for legibility
(the space is not a command or a value or a register name)"
I honestly don't know where to start... Any help is greatly appreciated, thanks guys.
As it is an assignment and you do not know where to start here are some hints:
Start with reading the stdin and split it into tokens
implement the stack to store operands and results
implement a few operations like +,-,*,/
make it all working
then implement the missing features one by one
#Serge has given a good outline of how to start, but you seem a bit at sea about just what dc does, so here are a few pointers for that.
The assignment says that you should experiment with the real dc program. Do this, if you haven't already done so, and you should learn quite a bit. (If you haven't done this, why not?)
Check out the Basic Operations section of the Wikipedia page on dc.
Read the dc man page. You'll find it confusing the first time, but read it again and try out what you see there with the actual program.
If you're confused by the whole idea of postfix notation see the Wikipedia page on Reverse Polish Notation, which is another name for the same thing.
This should let you understand the following very simple examples, as well as predict what they do:
3 p
3 4 * p
3 4 5 + * p
3 4 + 5 * p
Your assignment goes quite a way beyond, but if you get this much implemented you'll be more than half-way there.

Test Cases of atof() function in structure format

Two days ago i attended an interview.I had been asked a question and i am still finding answer.The Question Was tell me the test cases of atof(const char *str) function in c.I told him various test cases like
I have to check the given string should contain only numeric.
Given string contain one decimal point.
it should not overflow after conversion.
string should not be null.
but interviewer was not satisfied and asking for give me the answer in structured format.now my question is how to represent this answer in structure format so that in future i could not make same mistake.
I'm not sure what the interviewer means by "structured format", but I would do this by writing down the BNF syntax for floating point numbers (the C language specifies them), and then presenting test cases that test for each path through the syntax. Your cases notably do not cover the sign or exponent, and the number need not contain a decimal point.
A structural approach breaks the problem down into subproblems. Syntax is one subproblem, and the syntax chart or BNF provides a natural way to break that down into subproblems. An additional subproblem is boundary conditions ... there should be test cases for the minimum (> 0) and maximum valid values. There should also be test cases for handling of invalid inputs, but as lundin noted in a comment, that's impossible for atof as the behavior for invalid inputs is undefined.
Maybe you can structure your answer by what you are testing, like giving bad formated string (null, empty, etc ...) and by giving bad arguments like bad "numbers" (0 prefix/suffix 2.0, 0.4 etc...) you can also tests negative float numbers, put more than one dot in the string or whatever. I hope i have answer your question, if not, i think i haven't understood the question well.
I understand the term "test cases" differently than you.
I think what he wants are various inputs to atof and their expected results. For example:
1. atof("1.5") should return 1.5.
2. atof("-7") should retutn -7.0.
3. atof("Hello, world") should fail. But following Lundin's comment, there's no defined failure behavior for atof, so you can't really test this.
The test cases should cover all the different things the function needs to test. But you don't need to write down these things - just the example inputs and expected outputs.
Writing this in a structured format is easy.
We used use atof in our code most of the time we need to handle Internationalization/Localization in many languages 10.0 get converted to 10,0.
before calling atof you need to set locale and after completing the functionality you have to reset the locale.

What does the "k" stand for in Cocoa constant names [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What does the 'k' prefix indicate in Apple's APIs?
Objective C - Why do constants start with k
For example, the result codes defined for Audio Format Services:
kAudioFormatUnspecifiedError
kAudioFormatUnsupportedPropertyError
etc...
What does that leading k stand for? I've always assumed key, since such constants are often used as the keys in dictionaries, but those result codes are an example of where the constant is just a return value, not (as far as a client of the API can determine) a key.
I imagine that it merely stands for 'k'onstant, where 'k' is used because 'c' is already commonly used to indicate class or in Hungarian notation character.
The usage has historical precedent; early pocket calculators used 'k' to indicate constant mode (where repeated operation of = repeated the last operation) because 'c' was used for clear.
You can find the answer here.
Answer one
Constant names (#defines, enums, const local variables, etc.) should start with a
lower-case k and then use mixed case to delimit words, i.e.
kInvalidHandle, kWritePerm.
Though a pain to write, they are absolutely vital to keeping our code
readable. The following rules describe what you should comment and
where. But remember: while comments are very important, the best code
is self-documenting. Giving sensible names to types and variables is
much better than using obscure names and then trying to explain them
through comments.
But it has since been removed in the live version of the document. It
should be noted that it goes against the the Official Coding Guidelines
for Cocoa from Apple.
Cocoa coding guidelines
It was the coding standard left over when Apple used pascal. K was the prefix as opposed to all caps for other C languages.

Do Perl's sigils have anything to do with memory allocation? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Why do Perl variables need to start with $, %,#?
Other scripting languages seems to get along just fine without this or something similiar.
I guess it has something to do with memory allocation and helping the interpreter in order to speed things up, but I couldn't find anything specific on it. $scalar would probably be put into stack, #array into heap and %hash? Into heap as well? And what about ?subroutine?
Could someone help me figure this out or point me to some documentation? I am still trying to grasp some fundamentals and understand how everything works under the hood...
Because it makes it easier to read.
You know which identifiers are nouns, and whether they're singular or plural, because of the sigaldry. It's the same reason in English we have singular and plural determiners and agreement, as in this species is vs these species are. It's nice to know which is which.
Perl stores all data associated with a name in a single symbol table entry. The structure stored there is called a typeglob. The values for $foo, #foo, %foo, and &foo (subroutine reference) are all stored in the typeglob for "foo". The entire typeglob is denoted *foo (where * indicates "all sigils"). All this is explained in the perldata section of the Perl documentation.

Resources