How to make a Lex/Yacc parser which accepts navigation keys? - yacc

I've created a simple parser using Lex/Yacc.
My problem is, when I run this parser and type text into the console, each time I press the left arrow key, the cursor doesn't move to the left as expected, instead I get this strange group of characters: "^[[D"
Do you have any idea how to make the left arrow key works as expected?
Thank you.

This is what is happening: The arrow key generates a sequence of characters, not single character. THey are ancient VT100 codes. The three character sequence [A is up, [B is down, [C is right, and [D is left.
Some applications (like KSHELL) handle these escape sequences. When you do a left arrow, they know to send an escape sequence back to the terminal/terminal window that move the cursor left and they internally move the position of the insert location.
Such applications also do single character input.
If you just do plain vanilla C/C++ or other high level language read operations, you do not have the functionality available to you. Your generic C/C++ input does not return data to you charater-by-character. Instead, it buffers the data until you type . In other words, this is not a YACC/LEX problem but rather a general input programming problem.
If you want editing functionality, you need to us something that will perform character level input and process the escape sequences.
As the comments say, you need to use some library that will handle this for you.

Related

VIM equivalent of IntelliJ's expand/shrink selection?

How would one achieve the same result. I believe the keybinding for macOS Intellij is op+up/down and on windows it is alt+w/d.
Essentially the function highlights the current word, then, with successive presses, expands out to the full string/line/area in-between parenthesis/further out to the next set of parenthesis. Very useful for developing in LISP.
The closest I've gotten is this: https://vi.stackexchange.com/a/19028
Try this plug in: https://github.com/terryma/vim-expand-region
It expands selections based on Vim’s text objects.
Well this may seem comfortable but does not correspondent with the internal logic of vim itself.
See, in vim everything you enter is like a sentence. va{ for example: there is a verb v -> visually select and an object (or movement) { -> paragraph. In this case there is also a modifier a around. You can exchange stuff in this sentence and it will still work vaw, dil, cB and so on. The power of vim is greatly based on that concept.
Of course you can write a function that does vaw first, then S-v and lastly va{ but that will only work with visual selection. It will not work with c or d or anything. So I will recommend to get used to use different keys for different actions.
The visual selection is mostly not needed anyway. Change a paragraph? directly use ca} and so on.
I have found that VI/VA + WOBO (as many times as you need to expand) works similarly. Not as fast but its the same concept and you can even expand/shrink asymmetrically based on your WO's and BO's (Or OW's and OB's depending on how you look at it)

Replacing first and last character of every word using REGEXP_REPLACE

My question is somewhat specific, I'm not using any kind of code compiler to achieve the result in the title, I am using a IRC Client that allows the use of "Quirks" so the users can have specific mannerisms when chatting, like starting every word with an uppercase, or changing every "s" into a "2".
Problem is that I can't see the whole code so even though I'm not familiar with REGEXP_REPLACE it makes things harder to learn.
The client simplifies the whole coding process, here's a screenshot of the
interface
Filling the text boxes with "^(\w)" and "upper(\1)" respectively makes the first character capitalized, "(\w)$" and "upper(\1)" does the same with the last character.
I've discovered that "\b(\w)" will uppercase the first character of every word, i've tried "\b(\w)%" for the last character but it didn't work, as there is some syntax error, probably...
So, how do I get every last character capitalized?
1:

Pharo: customizing smart characters

The checkbox "Smart Characters" in "Code Completion" section of Settings Browser does (at least) two things:
1) It doubles some characters when typed: ', ", (, [, {
2) It enables that I can select a piece of code, press ( (i.e. Shift+9), and the selected code becomes surrounded by parentheses: (). I also can remove parentheses by pressing ( again. I also can do this with [] by pressing [ and with {} by pressing {, i.e. Shift+[.
I do not like the first of these things so I want to disable it, but I like the second thing and want to keep it. How can I achieve this? Turning off the checkbox will disable both.
P.S. I know that when the checkbox is off, adding/removing parentheses works by Cmd+Shift+9 (which is less convenient than Shift+9) and that Cmd+[ works for [], although I do not know any working shortcut for adding/removing {} when the checkbox is off.
The setting is called "Smart Characters", which should give you a clue as to where to look. Open a Finder, type in smartCharacters, and hit Enter. You should see some partial matches as well as an exact match for NECController and NECPreferences class (and the former just calls the latter). If you investigate the classes involved a bit, you'll see that smartCharacters stores a boolean, and that smartCharactersMapping returns a dictionary mapping some characters to their "counterparts", i.e. $[ to $] and so on. Now look at senders of smartCharactersMapping, and you'll see where it's being called from.
The caller you're most likely interested in would be NECController>>smartCharacterWithEvent. So put a breakpoint in that very ugly method to see what it does. You don't care about the first two cases (the editor having a selection and there not being a smart mapping), since you want to prevent the second matching character from being inserted. So the interesting bit for you is this bit:
self newSmartCharacterInsertionStringForLeft: char right: opposite
The method only has one implementor and that one sender, so it should be safe to comment out the original method and just return the "left" character, i.e.:
newSmartCharacterInsertionStringForLeft: left right: right
^String with: left
In other words, instead of creating a string with the left and right characters, and possibly spaces between them, just return a new string with the single character you typed. Might not be the most elegant way of solving this, but it should work, and should show you how to solve similar problems in the future.
(Ideally, you'll find a better solution, post it here as an alternate answer, and contribute it to the Pharo codebase - Pharo is open source, after all.)

Objective-C: How to use both "." and "," as a decimal separator or at least convert one to another on-the-fly

I have an instance of NSTextField, e.g. someTextField, for which I will use the number formatter to limit the input to numbers only.
The problem comes with the localization combined with the specific keyboard layouts used.
I would like to allow both the, say, American and European users to enter their localized decimal separators. As you all know, in the USA that would be . and for the good part of Europe that would be , (and similar with the thousands separator, etc. but let's put that to the side for now).
So I wrote the following task:
[numberFormatter setLocale:[NSLocale currentLocale]]; for the instance of the NSNumberFormatter.
Problems occurs when the user who has , set as a decimal separator AND US keyboard layout switched on (fairly common here in Europe) presses the decimal separator key on the numeric keyboard. With the US keyboard layout on, that would give him the . as the decimal separator but at the same time it'll be ignored in the someTextField because of the localized settings system-wide. So, if you want to type 1/2 using numeric keyboard only, you'll type 0.5 (US keyboard layout) in the text field and it would be read by the system as 0 because it recognizes only , as decimal separator. This is how the program currently is working and I would like to improve it in this regard.
I would like to allow user to type in the . in the someTextField and for the system to recognize it as a decimal separator just like it would ,. This kind of behavior can be seen in Apple's own Calculator application. If you type . on the numeric keyboard it'll appear as , immediately on the screen (for all conditions as described previously).
Question is: is it possible for me to achieve this using an instance of NSNumberFormatter? If not, is it possible to set on-the-fly conversion of the numerical keyboard decimal separator key output to the decimal separator set system-wide? Or perhaps you have some other suggestions?
Thanks.
I don't have a specific answer to your question, but I'd say the right approach is not to muck about with the NSNumberFormatter at all and concentrate on trying to change the characters generated by the keyboard.
The default locale for number formatters is usually the system's default locale as set by the user in the internationalization settings. If you change that behaviour programmatically for UI elements, you are effectively telling the user "I know better than you how you want to input numbers". Arrogance of that sort on the part of the developer never gets them good marks with respect to UI design.
In fact, you could apply the same argument to remapping the dot button on the numeric keypad. How do you know that the user hasn't set US keyboard layout because it allows them to get a dot from that key? Maybe they consider it more important to be able to type the thousands separator from the keypad than the decimal separator. I'm not saying you shouldn't implement your feature, just make sure that the user has control over when it is enabled or disabled.
Anyway, you probably want to override the keyDown event on the control. More info here.
Take a look at the UITextFieldDelegate protocol. It allows your textfield to ask its delegate if it should accept a character which the user just typed. The apropriate method would be textField:shouldChangeCharactersInRange:replacementString. If the character in question is , or . just let the delegate append the properly localized decimal separator "manually" and return NO.
I'm not quite sure if this will work if the text field is set to number mode, maybe the input is being filtered before the delegate method is called - leading to the method not being called if the "wrong" separator has been filtered out previously. If so, you might want to consider leaving the text field in alphanumerical mode and use the delegate method again to filter out anything that is not numbers or separators. However, in this case you should make sure the user is not allowed to type more then one decimal separator - either ignore the surplus ones or remove the first one and accept the new one.

How can I validate text box input?

I am creating a program and I need to validate my text boxes. For the program the user needs to put in a phrase. But I am not sure how to make sure that the user actually entered in a phrase, the phrase isn't (ex.) skldkfdl, or that there isn't a space.
Strings in Java
You could do a String.Trim() to get rid of trailing whitespaces first...
then do a String.IndexOf(" ") to check for a space.
If the function returns -1, it means there is no space in the string.
Running on the assumption that you're using VB.Net - Add an event handler for the event where you want to validate the text, such as when a "Submit" button is clicked. You may want to use a CancelEventHandler, so that you can cancel the click.
In the event handler, if you're looking for just simple validation, you can use if-statements to check some simple conditions, such as if you just want to check "if input.equals(password)".
Look here for an example of using CancelEventHandler
If you're looking for some more complex validation, you'll want to use regular expressions.
This page might help get you started
Checking to see if something is "a phrase", as in, proper English, would be very difficult. You would need to make sure that all of the words are in the dictionary, and then you would need to check for proper grammar, which is incredibly complex, given English grammar rules. You may want to simplify your approach, depending on your problem. For example, maybe just check that no weird characters are used, that there is more than one space, and that each word contains a vowel.