lex/yacc won't recognize a string (syntax error) - lex

Hi i'm trying to let lex/yacc split this string
table subwayLines:int[3]
into tokens of table, subwayLines, int[3] with the [3] optional(i.e. int or int[3])
everything is fine until i try to recognize the "int",
so this is what i did in lex
[A-Za-z0-9\[\]]+ { /* column property*/
yylval.sval = (char *)strdup(yytext);
char* temp=yylval.sval;
return STRING;
}
i know the problem is in
[A-Za-z0-9\[\]]+
because when i changed it to
[A-Za-z]+("[")?+[0-9]+("]")?+(",")?
it works except I still can't go without the "[" or "]", for example, if i wrote this in my string:
table subwayLines:int
then it gives me a syntax error
so does anyone knows how to do change it? thanks

To make the [3] optional, this will not work:
[A-Za-z]+("[")?+[0-9]+("]")?+(",")?
You've made only the square brackets optional, but not the number in between. You need something like
[A-Za-z]+("["[0-9]+"]")?
I.e. the entire square-bracketed part is optional.
Also the combination (REGEX)?+ doesn't make much sense (the ?+ part of it). It's equivalent to (REGEX)*, since you're effectively saying that (REGEX) is optional, one or more times, which is like zero or more.
(Not sure why you have the optional comma in the second example; the first one doesn't recognize a comma and it's not shown in your example input.)

Related

Empty Character while looping

Hey i have a string which printed gives the following: BBBKKKKJJJJJJGGG​G
If I use this code:
for (index,character) in vin.characters.enumerated(){
print(character)
vinTextFields[index].text = String(character)
}
to split it into some textFields the index is out of range. This is because the for loop inserts an empty character.
This is what the print(character) gives me back.
I really not get why this empty line appears.
I tested it, it seems there is a character that is not printable between the last two 'G'.
I think you copied and pasted it from somewhere. That's how that character made it there.
Remove the last two 'G' and type them again and you will be good to go. I tested that too.

How to search for any unicode symbol in a character string?

I've got an existing DOORS module which happens to have some rich text entries; these entries have some symbols in them such as 'curly' quotes. I'm trying to upgrade a DXL macro which exports a LaTeX source file, and the problem is that these high-number symbols are not considered "standard UTF-8" by TexMaker's import function (and in any case probably won't be processed by Xelatex or other converters) . I can't simply use the UnicodeString functions in DXL because those break the rest of the rich text, and apparently the character identifier charOf(decimal_number_code) only works over the basic set of characters, i.e. less than some numeric code value. For example, charOf(8217) should create a right-curly single quote, but when I tried code along the lines of
if (charOf(8217) == one_char)
I never get a match. I did copy the curly quote from the DOORS module and verified via an online unicode analyzer that it was definitely Unicode decimal value 8217 .
So, what am I missing here? I just want to be able to detect any symbol character, identify it correctly, and then replace it with ,e.g., \textquoteright in the output stream.
My overall setup works for lower-count chars, since this works:
( c is a single character pulled from a string)
thedeg = charOf(176)
if( thedeg == c )
{
temp += "$\\degree$"
}
Got some help from DXL coding experts over at IBM forums.
Quoting the important stuff (there's some useful code snippets there as well):
Hey, you are right it seems intOf(char) and charOf(int) both do some
modulo 256 and therefore cut anything above that off. Try:
int i=8217;
char c = addr_(i);
print c;
Which then allows comparison of c with any input char.

In emacs, how do I force certain characters to act as end of statement delineators?

I've created a new major mode derived from cc-mode, because I'm using a meta-language that is mostly C-like, but is parsed to generate code automatically.
Say I have something like this:
struct MyNewStruct
{
int newInt = 32;
{
[flag, different-flag]
string newString = "foo";
}
}
I need the ']' character to effectively be equivalent to the ; or the next line, declaring the string, doesn't indent properly.
I've tried using M-x modify-syntax-entry for ']' and making it both a closing character as well as a punctuation character (according to the GNU manual on syntax tables), but it doesn't look like it's allowed to belong to two character classes simultaneously (unless one of those character classes is a comment). (And if it's just a punctuation character, that causes other problems.)
I can't change the grammar of the meta-language, so adding a semicolon after the close bracket isn't possible.
In this case, the real answer was to pick something that was syntactically closer to my meta-language. csharp-mode already parses the brackets correctly and marks sections enclosed in brackets as statements, not statement-cont.

Determine if non-numerical characters have been pasted into UITextField

For a specialized calculator I would like to allow copy / paste for a textfield which is meant for numerical values only. So, only numerical characters should be actually pasted or the pasted string should be rejected if it contains non-numerical characters.
I was thinking about using UITextFieldDelegates textField:shouldChangeCharactersInRange:replacementString: method to check the pasted string for non-numerical characters. But NSString offers no method for checking whether it does NOT contain characters specified in a single set. So this way I would need to check occurances of characters from several sets, which is clumsy and these checks would run for every single number that would be typed in, which appears like quite some overhead to me.
Another way would be to iterate and check for every character in the replacement string whether there's a match in a numerical set.
Either way would propably work, but I feel like I'm missing something.
Do you have any advice? Is there a convenience method to achieve this?
But NSString offers no method for checking whether it does NOT contain characters specified in a single set
sure it does.
if([myString rangeOfCharacterFromSet:myCharacterSet].location ==NSNotFound)
{
//means there is no character from specified set in specified string
}

String to Unicode in C#

I want to use Unicode in my code. My Unicode value is 0100 and I am adding my Unicode string \u with my value. When I use string myVal="\u0100", it's working, but when I use like below, it's not working. The value is looking like "\\u1000";. How do I resolve this?
I want to use it like the below one, because the Unicode value may vary sometimes.
string uStr=#"\u" + "0100";
There are a couple of problems here. One is that #"\u" is actually the literal string "\u" (can also be represented as "\u").
The other issue is that you cannot construct a string in the way you describe because "\u" is not a valid string by itself. The compiler is expecting a value to follow "\u" (like "\u0100") to determine what the encoded value is supposed to be.
You need to keep in mind that strings in .NET are immutable, which means that when you look at what is going on behind the scenes with your concatenated example (`#"\u"+"0100"), this is what is actually happening:
Create the string "\u"
Create the string "0100"
Create the string "\u0100"
So you have three strings in memory. In order for that to happen all of the strings must be valid.
The first option that comes to mind for handling those values is to actually parse them as integers, and then convert them to characters. Something like:
var unicodeValue = (char)int.Parse("0100",System.Globalization.NumberStyles.AllowHexSpecifier);
Which will give you the single Unicode character value. From there, you can add it to a string, or convert it to a string using ToString().