is there a way to have clang format to require (or not require), parentheses around the argument? - clang-format

Want clang-format to enforce a C language return keyword style. I'd prefer:
return (foo);
but can live without the parentheses, I just want clang-format to do enforcement. -- Thanks!

Related

Errors in definitions in Flex and Lex

I am writing a lexical analyser for a toy programming language with toy keywords. I wish to print "keyword" for every keyword the analyser bumps into. To make my code cleaner, I defined the term "keyword" for all keywords above the rule section.
%{
#include <stdio.h>
%}
keyword program | begin | ... | end
where the ... implies rest of the keywords.
In the rules section, I wrote the following rule:
{keyword} {
printf("keyword\n");
}
Then finally I wrote the main function and yywrap function.
However, when I compile the generated lex.yy.c file, I get the following error.
use of undeclared identifier 'keyword'
{keyword} {
^
Please help me with this error, I am new to this scanner-generating language.
You will get better answers here if you copy and paste the precise text of your program into your question. Otherwise, you force anyone answering to guess what the original text is. This is my guess:
Probably the line that is being complained about was indented in your flex input file. Make sure that all rules start exactly at the left margin. (Any indented text is copied verbatim into the output file, as though it were C code. The most common use for this feature is to add comments to your Flex rules.)
Also, you cannot use unquoted spaces in a macro definition; you would need:
keyword program|begin|...|end
Otherwise, flex will throw an error when it expands the macro. (It didn't expand the macro in this case, presumably because of the first problem.)

How to prevent clang-format from moving braces to a different line?

In the following snippets I have used the "K & R" style bracing for the "if" body and the "brace under" style for the "else" body. I realize that good practice requires that these or any other bracing styles be consistent, but I have a good reason for wanting to keep them the way they are.
The problem is that clang-format doesn't seem to have an option to not change brace styles to the style that has been selected (LLVM, Google, etc.) Instead, it will move any braces that do not conform to that style to different lines if necessary.
The clang-format documentation states that I can use the comments /* clang-format off / and / clang-format on */ to disable formatting within a delimited range. I believe that's what I've done in the second version of the snippet below, but the braces still get moved to match the selected style anyway. Is there any way to prevent the formatter from formatting specific items?
if (1 < 2) {
;
}
else
{
;
}
if (1 < 2) /* clang-format off */{/* clang-format on */
;
/* clang-format off */}/* clang-format on */
/* clang-format off */else/* clang-format on */
/* clang-format off */{/* clang-format on */
;
}

flex scanner push-back overflow with automata

I am having a hard time with this problem.
"Write a flex code which recognizes a chain with alphabet {0,1}, with at least 5 char's, and to every consecutive 5 char's there will bee at least 3 1's"
I thought I have solved, but I am new using flex, so I am getting this "flex scanner push-back overflow".
here's my code
%{
#define ACCEPT 1
#define DONT 2
%}
delim [ \t\n\r]
ws {delim}+
comb01 00111|{comb06}1
comb02 01011|{comb07}1
comb03 01101|{comb08}1
comb04 01110|({comb01}|{comb09})0
comb05 01111|({comb01}|{comb09})1
comb06 10011|{comb10}1
comb07 10101|{comb11}1
comb08 10110|({comb02}|{comb12})0
comb09 10111|({comb02}|{comb12})1
comb10 11001|{comb13}1
comb11 11010|({comb03}|{comb14})0
comb12 11011|({comb03}|{comb14})1
comb13 11100|({comb04}|{comb15})0
comb14 11101|({comb04}|{comb15})1
comb15 11110|({comb05}|{comb16})0
comb16 11111|({comb05}|{comb16})1
accept {comb01}|{comb02}|{comb03}|{comb04}|{comb05}|{comb06}|{comb07}|{comb08}|{comb09}|{comb10}|{comb11}|{comb12}|{comb13}|{comb14}|{comb15}|{comb16}
string [^ \t\n\r]+
%%
{ws} { ;}
{accept} {return ACCEPT;}
{string} {return DONT;}
%%
void main () {
int i;
while (i = yylex ())
switch (i) {
case ACCEPT:
printf ("%-20s: ACCEPT\n", yytext);
break;
case DONT:
printf ("%-20s: Reject\n", yytext);
break;
}
}
Flex definitions are macros, and flex implements them that way: when it sees {defn} in a pattern, it replaces it with whatever defn was defined as (in parentheses, usually, to avoid operator precedence issues). It doesn't expand the macros in the macro definition, so the macro substitution might contain more definition references which in turn need to be substituted.
Since macro substitution is unconditional, it is not possible to use recursive macros, including macros which are indirectly recursive. Which yours are. Flex doesn't check for this condition, unlike the C preprocessor; it just continues substituting in an endless loop until it runs out of space.
(Flex is implemented using itself; it does the macro substitution using unput. unput will not resize the input buffer, so "runs out of space" here means that flex's internal flex's input buffer became full of macro substitutions.)
The strategy you are using would work fine as a context-free grammar. But that's not flex. Flex is about regular expressions. The pattern you want to match can be described by a regular expression -- the "grammar" you wrote with flex macros is a regular grammar -- but it is not a regular expression and flex won't make one out of it for you, unfortunately. That's your job.
I don't think it's going to be a very pretty regular expression. In fact, I think it's likely to be enormous. But I didn't try working it out..
There are flex tricks you could use to avoid constructing the regular expression. For example, you could build your state machine out of flex start conditions and then scan one character at a time, where each character scanned does a state transition or throws an error. (Use more() if you want to return the entire string scanned at the end.)

Dart regular expression using `$1`

I want to use $1 in adart regular expression to get the grouping in the expression.
Just like javascript regular expressions.
void main() {
// javascript
// 'hello-world'.replace(/-(w)/, '+$1'); // hello+world
print('hello-world'.replaceAll(RegExp(r'-(w)'), '+\$1')); // hello+$1orld
}
Completely unexpected.
Expected result hello+world, actual resulthello+$1orld
The Dart replaceAll method does not treat its second argument as a replacement pattern.
Rather than introduce a new language for specifying replacements in a string, Dart allows you to use a normal Dart expression to compute the replacement.
Dart has a short syntax for functions, so this is not much longer than the string, but it allows you to insert arbitrarily complicated computations when necessary.
So, use the replaceAllMapped function and write the replacement as follows:
print('hello-world'.replaceAllMapped(RegExp(r'-(w)'), (m) => '+${m[1]}'));
For this particular example, the RegExp is so simple that you can replace with a plain string:
print('hello-world'.replaceAll(RegExp(r'-(?=w)'), '+'));
This replaces a - with a + only when the - is followed by a w.

Any Lua pattern alternative to the regex (\\.|.)?

There's a common idiom for traversing a string whose characters may be escaped with a backslash by using the regex (\\.|.), like this:
alert( "some\\astring".replace(/(\\.|.)/g, "[$1]") )
That's in JavaScript. This code changes the string some\astring to [s][o][m][e][\a][s][t][r][i][n][g].
I know that Lua patterns don't support the OR operator, so we can't translate this regular expression directly into a Lua pattern.
Yet, I was wondering: is there an alternative way to do this (traversing possibly-escaped characters) in Lua, using a Lua pattern?
You can try
(\\?.)
and replace with [$1]
See it on Regexr.
? is a shortcut quantifier for 0 or 1 occurences, so the above pattern is matching a character and an optional backslash before. If ? is not working (I don't know lua) you can try {0,1} instead. That is the long version of the same.

Resources