How should is use yy_scan_buffer in (.y) file( lex and yacc) - yacc

How should is use yy_scan_buffer in (.y) file( lex and yacc). The return type of yy_scan_buffer is YY_BUFFER_STATE which is in lex.yy.c .

For background: it is an special function called from yy_scan_bytes, which in turn is called from yy_scan_string (likewise can be overridden).
According to String input to flex lexer, the return-type corresponds to a handle which should be deleted using yy_delete_buffer, but that yy_scan_buffer does the deletion. However (looking at the generated code), that does not appear to be correct — perhaps this depends upon the version of flex which is used.
According to these questions, you might want to use yy_scan_string, etc., in writing reentrant code (although the functions predate any work on flex to provide reentrancy):
how to use yy_scan_string(const char *str) (generated by lex yacc) in a separated file
how to use yy_scan_string in lex
Flex's current documentation mentions its use for multiple input buffers:
Some scanners (such as those which support “include” files) require reading from several input streams. As flex scanners do a large amount of buffering, one cannot control where the next input will be read from by simply writing a YY_INPUT() which is sensitive to the scanning context. YY_INPUT() is only called when the scanner reaches the end of its buffer, which may be a long time after scanning a statement such as an include statement which requires switching the input source.
The documentation goes on to provide examples of usage. Depending on what you want to do, those may be useful.

Related

Find MMT Unicode abbreviations for given symbol (e.g. given ☞, find "juri")

The usual IDEs/editors for MMT (e.g. IntelliJ + MMT plugin or jEdit) feature an autocompletion feature for certain useful Unicode characters. For instance, I can type jle and immediately get suggested jleftrightarrow that, upon autocompletion, is replaced by ↔.
Is there a way to find out the reverse association? E.g. I have the symbol ☞ at hand and would like to know the autocompletion abbreviation starting with j — if it exists. For that hand, I would get juri.
The MMT OnlineTools I developed allow this: https://comfreek.github.io/mmteditor.
See screenshot below: if you already have a string full of Unicode symbols that you don't know how to type, just paste it under "how do I type X?". And if you are looking for a specific abbreviation — by Unicode character or by (parts of its) name — use the "abbreviation search" feature.
Internally, my tools pulls from (a copy of) the same resource file that Dennis linked in his answer.
As far as I know, there currently isn't a good way to look up or search for the ASCII abbreviations, except to go straight for the source — which at least has the advantage that it's guaranteed to be up-to-date.
The IDE plugins all have access to an mmt.jar and load their abbreviations from a specific resource file embedded therein. You can find it here on GitHub: https://github.com/UniFormal/MMT/blob/master/src/mmt-api/resources/unicode/unicode-latex-map.
In the long term, we should consider extending that file with a third "field" that gives a short description, and e.g. have a text field in IntelliJ to search for a specific abbreviation.

Search all programs within a package for a MODIFY statement

I want to search all programs - within a package - that use the statement:
modify itab_xyz from wa_itab_xyz
Preferably, the string should be searched with wild cards like itab*
for a range of itab_(values) like itab_abc, itab_def, itab_ghi
etc..
How do i do this in SAP ABAP?
Below is a screenshot of all programs within a package one can search from.
One possibility would be to use program RS_ABAP_SOURCE_SCAN.
You can restrict the selection by package and you can also enter a specific string to search for in the code.
I use the transaction code_scanner (program is afx_code_scanner).
The biggest problem with this program and the RS_ABAP_SOURCE_SCAN provided above is that they won’t find everything. IMO the most important missing component to them is implicit enhancements. They can be very impactful to system functions, and if you are searching for an error message or hard coded value skipping them could mean not finding something critical.
At the time I looked (about 7 years ago), I was unable to find a delivered tool that would actually scan all the code in the system. I ended up enhancing the code_scanner to look for enhancements, WDA components, BSP code, and forms code.
I don’t know if the open source component above includes those. At first glance it doesn’t seem to, but I don’t have time to really dig into it.
You could use a tool from the Galileo-Open Source library. This program searches ABAP Source, OTR-Texts, Message and Textpools for static Text, wildcard patterns or regex patterns.
ABAP-Coding:
https://github.com/galileo-group/galileo-abap-lib/blob/master/%23gal%23devtools_find_text.prog.abap
Textpool:
https://github.com/galileo-group/galileo-abap-lib/blob/master/%23gal%23devtools_find_text.prog.xml
It refers to some additional classes from the library, so you either need to copy these as well or just use ABAPgit to get the whole library. You can also contact me, so I can send you a transport containing the library.
Additional information (October 1, 2020):
I created a version of the report that you can copy/paste to the ABAP editor. It is too long to include it in the response, but you can find it here.
Do not forget to copy the text elements / selection texts.
Required Text Elements:
-----------------------
B00 Scope
B01 Search pattern
H01 Type
H02 Name
H03 Key
H04 Match
Required Selection Texts:
-------------------------
P_CASE Case-sensitive
P_DEVC Package
P_LANGU Language
P_MESS Messages
P_OTR OTR Texts
P_PATT Pattern
P_REGEX Regular expression
P_SOURCE ABAP sources
P_TPOOL Textpools
P_WILDC Wildcard pattern

Is it possible to preserve variable names when writing and reading term programatically?

I'm trying to write an SWI-Prolog predicate that applies numbervars/3 to a term's anonymous variables but preserves the user-supplied names of its non-anonymous variables. I eventually plan on adding some kind of hook to term_expansion (or something like that).
Example of desired output:
?- TestList=[X,Y,Z,_,_].
> TestList=[X,Y,Z,A,B].
This answer to the question Converting Terms to Atoms preserving variable names in YAP prolog shows how to use read_term to obtain as atoms the names of the variables used in a term. This list (in the form [X='X',Y='Y',...]) does not contain the anonymous variables, unlike the variable list obtained by term_variables, making isolation of the anonymous variables fairly straightforward.
However, the usefulness of this great feature is somewhat limited if it can only be applied to terms read directly from the terminal. I noticed that all of the examples in the answer involve direct user input of the term. Is it possible to get (as atoms) the variable names for terms that are not obtained through direct user input? That is, is there some way to 'write' a term (preserving variable names) to some invisible stream and then 'read' it as if it were input from the terminal?
Alternatively... Perhaps this is more of a LaTeX-ish line of thinking, but is there some way to "wrap" variables inside single quotes (thereby atom-ifying them) before Prolog expands/tries to unify them as variables, with the end result that they're treated as atoms that start with uppercase letters rather than as variables?
You can use the ISO core standard variable_names/1 read and write option. Here is some example code, that replaces anonymous variables in a variable name mapping:
% replace_anon(+Map, +Map, -Map)
replace_anon([_=V|M], S, ['_'=V|N]) :- member(_=W, S), W==V, !,
replace_anon(M, S, N).
replace_anon([A=V|M], S, [A=V|N]) :-
replace_anon(M, S, N).
replace_anon([], _, []).
variable_names/1 is ISO core standard. It was always a read option. It then became a write option as well. See also: https://www.complang.tuwien.ac.at/ulrich/iso-prolog/WDCor3
Here is an example run:
Welcome to SWI-Prolog (threaded, 64 bits, version 7.7.25)
?- read_term(X,[variable_names(M),singletons(S)]),
replace_anon(M,S,N),
write_term(X,[variable_names(N)]).
|: p(X,Y,X).
p(X,_,X)
To use the old numbervars/3 is not recommended, since its not compatible with attribute variables. You cannot use it for example in the presence of CLP(FD).
Is it possible to get (as atoms) the variable names for terms that are not obtained through direct user input?
if you want to get variable names from source files you should read them from there.
The easiest way to do so using term expansion.
Solution:
read_term_from_atom(+Atom, -Term, +Options)
Use read_term/3 to read the next term from Atom.
Atom is either an atom or a string object.
It is not required for Atom to end with a full-stop.
Use Atom as input to read_term/2 using the option variable_names and return the read term in Term and the variable bindings in variable_names(Bindings).
Bindings is a list of Name = Var couples, thus providing access to the actual variable names. See also read_term/2.
If Atom has no valid syntax, a syntax_error exception is raised.
write_term( Term ) :-
numbervars(Term, 0, End),
write_canonical(Term), nl.

gulp-newer vs gulp-changed

What're the differences between them?
gulp-newer:
gulp.src(imgSrc)
.pipe(newer(imgDest))
.pipe(imagemin())
.pipe(gulp.dest(imgDest));
gulp-changed:
gulp.src(SRC)
.pipe(changed(DEST))
// ngmin will only get the files that
// changed since the last time it was run
.pipe(ngmin())
.pipe(gulp.dest(DEST));
It seems gulp-changed is more powerful, because it provides an option
hasChanged: changed.compareLastModifiedTime
I hope it's not too late to answer this question. I have had to evaluated both of them at a source-code level for a recent project, and here is my take.
gulp-newer
At the core, this plugin compares the source and dest file's modified time (see node API) to decide whether the source file is newer than the dest file or if there is no dest file at all. Here is the related code in the plugin:
var newer = !destFileStats || srcFile.stat.mtime > destFileStats.mtime;
gulp-changed
This plugin by default also uses a file's modified time to decide which to pass through the stream
function compareLastModifiedTime(stream, cb, sourceFile, targetPath) {}
but it goes one step further by offering an option to compare the file's content SHA1 hash:
function compareSha1Digest(stream, cb, sourceFile, targetPath) {}
This information is nicely documented.
Conclusion
So theoretically speaking, if you use gulp-changed's default hasChanged: changed.compareLastModifiedTime, each plugin is relatively as fast as the other. If you use gulp-changed's hasChanged: changed.compareSha1Digest, it's reasonable to expect gulp-changed to be a bit slower because it does create a SHA1 hash of the file content. I didn't benchmark but I'm also interested in seeing some number.
Which to choose
gulp-changed, purely because of the developer behind it (sindresorhus). If one day this awesome man decides that he will stop supporting his gulp plugins, I think I will stop using gulp altogether.
Joking aside, though, gulp-changed's source code is gulp-y, while gulp-newer's source reads pretty much like just another node module's source with lots of promises. So another +1 for gulp-changed :)
HUGE EDIT
Gulp-changed only works with 1:1 source:dest mapping. If you need many:1, e.g. when using with gulp concat, choose gulp-newer instead.
May I suggest gulp-newy in which you can manipulate the path and filename in your own function. Then, just use the function as the callback to the newy(). This gives you complete control of the files you would like to compare.
This will allow 1:1 or many to 1 compares.
newy(function(projectDir, srcFile, absSrcFile) {
// do whatever you want to here.
// construct your absolute path, change filename suffix, etc.
// then return /foo/bar/filename.suffix as the file to compare against
}
In order to answer this question you will have to compare both plugins source code.
Seems that gulp-changed has more options as you have said, more used (it was downloading more time) and more contributors, thus, it could be more updated and refactored, as it was being used more.
Something that can make a difference, due to them documentation.
On the example, for gulp-newer, its used like this:
gulp.task('default', function() {
gulp.watch(imgSrc, ['images']);
});
Thus, seems that once this task is running, it will only notice files that are changing while you are using this plugin.
On gulp-changed, they say: "will only get the files that changed since the last time it was run". So, and I didnt try this on a working example, that gulp-changed proccess all files and then only the ones that have been changed since last execution, so seems it will always "look" at all files and internally (md5 hash? no clue, didnt check the source) decide whereas a file has changed since last execution. Do not need a watcher for that.
All this, was only reading their official documentation.
A "on the wild test" would be very welcomed !

software\tool for checking syntax format

Im looking for a tool that can validate if a given text\paragraph subject to a specific format .
for example :
I can be able to check if the text is as following :
xxx{
sss:aaa;
}
yyy();
preferably open source tool, with easy rule sets like xml or something .
by text i mean a string that i get from i.e fgets(), or any function that reads from a file .
For something like this I'd suggest a parser (see, for instance, What is Parse/parsing?). You can build one from a definition of the language that you want to parse using a parser generator like Yacc or its free GNU equivalent Bison, or any number of other parser generators, many of which are also freely available.
Most parsers are used to transform a text that complies with a grammar into some other form (e.g. an intermediate language or a machine code) but that isn't neccesary - in your case the parser could simply say (at a minimum) "Yes" if the text conforms to a given grammar.
Parsers for simple grammars can be built by hand but, if you have the tools available, using a parser generator is easier and more robust in my experience.
Further, the text that you've shown is similar to a portion of code written in the C language (something close to a struct declaration followed by a function call), so you would be able to re-use parts of the grammar that you need from an existing Yacc grammar for C like this one.